Table colours fail when extra row added

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Table colours fail when extra row added

Post by synical21 »

Hey Gurus,

I got a weird problem with formating the colour of a table made in php. Everything works fine when i use 5 rows each row has an alturnative colour, but when i add the 6th row the colour fails and every row is the same colour which is the first colour (green). I used the same technique on every row so i dont understand how it fails with an extra row. Heres the code:

Code: Select all

 
 $result = mysql_query("SELECT * FROM fulldata WHERE approved = '0'") or die(mysql_error() );  
    $i=1;
    $row = mysql_fetch_array($result);
    $line = mysql_fetch_assoc($result);
 
// headers of the table
echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>";
      echo "<tr><td align='left' width='40%'><b>Description</b></th><td align='center' width='30%'><b>Proof</b></th><td align='center' width='10%'><b>Workers Needed</b></th><td align='center' width='10%'><b>Price</b></th><td align='center' width='10%'><b>Accept</b></th><td align='center' width='10%'><b>Deney</b></th></tr>";
// ITERATE OVER THE RESULTS SET
    while ($line = mysql_fetch_assoc($result)) 
    {
// GET EASY-TO-READ LOCAL VARIABLES    
        foreach ($line as $key => $val) { $$key = htmlentities($val); }
// CREATE THE ROW OF DATA  
$color = ($i % 2) ? "#3F6" : "#3CF";
       echo "<tr>";
         echo "<td style='background-color:$color' align='center'>$descript</td>\n";
          $i++;
         echo "<td style='background-color:$color' align='center'>$proof</td>\n";
          $i++;
          echo "<td style='background-color:$color' align='center'>$amountworkers</td>\n";
          $i++;
        echo "<td style='background-color:$color' align='center'>$perperson</td>\n";
         $i++;
        echo "<td style='background-color:$color' align='center'>Accept</td>\n";
         $i++;
         echo "<td style='background-color:$color' align='center'>Deney</td>\n";
         $i++;
    } 
      // END WHILE ITERATOR
    echo "</table>\n";
 // END IF/ELSE
        
 
         ?>
 
 
that last row
echo "<td style='background-color:$color' align='center'>Deney</td>\n";
$i++;
when added ruins all the colours. when taken out the table works fine.

How can i fix this?
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Table colours fail when extra row added

Post by synical21 »

bump
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Table colours fail when extra row added

Post by requinix »

I take it you haven't thought much about this.

I count 6 $i++ statements in there. If $i=1 on the first row, what will it be on the second?
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Table colours fail when extra row added

Post by synical21 »

Sorry yes lol i didnt pay much attention i just googled what i needed to do and came across this piece of code, my bad. Fixed now.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Table colours fail when extra row added

Post by cpetercarter »

Also, you need to close the table row with </tr> before you close the table.
Post Reply