NameSilo

Alternating Row colours

Namecheap AuctionsNamecheap Auctions
Spacemail by SpaceshipSpacemail by Spaceship
SpaceshipSpaceship
Watch

asgsoft

VIP Member
Impact
9
Hello Everyone

I am making a script that generates 10 random links from a db, I want them to have a alternating row colour but I can't seem to get it right.

Look at this:

PHP:
<?php
include 'config.php';
mysql_connect("$host", "$user", "$pass") or die(mysql_error()); 
mysql_select_db("$dbname") or die(mysql_error());

// lets check to see if we have enough

$checkaff = mysql_query("SELECT * FROM links");

$aff_exist = mysql_num_rows($checkaff);
$maximage = 10;
$halfmaximage = floor($maximage/2);

if($aff_exist >= $maximage){

$query = "SELECT * FROM links";
$result = mysql_query($query) or die(mysql_error());
$i = 0;

while($row = mysql_fetch_array($result)){
$array['text'][$i] = $row['text'];
$array['link'][$i] = $row['link'];
$array['showing'][] = 0;
$i++;
}
echo "<table width='100%' border='0' cellspacing='2' cellpadding='0'>";
$ii = 0;
while($showing < $halfmaximage){
$ii++;
if($ii % 2){
$col = "#F4F4F4";
}else{
$col = "";
}
$randnum = rand(0,$i-1);
if($array['showing'][$randnum] == 0){
echo "<tr>
<td width='75%' bgcolor='$col'><a href='".$array['link'][$randnum]."'>".$array['text'][$randnum]."</a></td>
</tr>";
$array['showing'][$randnum] = 1;
$showing++;
}
}

while($showing2 < $halfmaximage){
if($ii % 2){
$col = "#F4F4F4";
}else{
$col = "";
}
$ii++;
$randnum = rand(0,$i-1);
if($array['showing'][$randnum] == 0){
echo "<tr>
<td width='75%' bgcolor='$col'><a href='".$array['link'][$randnum]."'>".$array['text'][$randnum]."</a></td>
</tr>";
$array['showing'][$randnum] = 1;
$showing2++;
}
}
}
?>

Everytime it just colours a randome row.

How can I fix that?
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
PHP:
$ii = 0;
while($showing < $halfmaximage){
  if($ii==0){
    $col = "#F4F4F4"; $ii++;
  } else {
    $col = ""; $ii--;
  }
 
0
Close
Just editing Jim's code:

PHP:
$ii = 1;
while($showing < $halfmaximage){
  if($ii % 2)
    $col = "#F4F4F4"; 
  else
    $col = "";
  $ii++;
}

Edit: Actually, it's not any shorter.. I thought it would be but that's only because I forgot a part.. :X
 
Last edited:
0
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back