Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.