Jump to content

[SOLVED] Row Bg change...


phpSensei

Recommended Posts

edit: I see I was too slow and not so elegant, but if you want to understand what's happening "behind" the ternary operators used in the above examples, take a look at mine. It's basically the same, only longer... :P

 


 

If you echo the table using a loop, all you need to do is add a counter and a check for whether the current number is odd or even, and apply a css class to the current row:

 

<?php
$sql = "SELECT * FROM table";
$result = mysql_query($sql, $connection);
$i = 1; // this is the counter - make sure you set it _outside_ the loop!
while ( $row = mysql_fetch_assoc($result) ) {
     if ($i % 2 = 0) { // we check to see if the counter is en even number. if so - blue background
          $style = "blue";
     }
     else { // if it's not, the background becomes red
          $style = "red";
     }
     echo "<tr style=\"background: $class\"><td>$row[]</td></tr>"; // print the row
     $i++; // make sure this addition happens _inside_ the loop!
}
?>

Link to comment
https://forums.phpfreaks.com/topic/69819-solved-row-bg-change/#findComment-350743
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.