suttercain Posted May 12, 2008 Share Posted May 12, 2008 Hi everyone, I have been able to do alternating colored rows using CSS and PHP which is like this: ROW 1 - Blue ROW 2 - Grey ROW 3 - Blue Row 4 - Grey and so on... question, is it possible to do this? Row 1 - Blue Row 2 - White Row 3 - White Row 4 - Grey Row 5 - White Row 6 - White Row 7 - Blue Row 8 - White Row 9 - White Row 10 - Grey Row 11 - White Row 12 - White I can't figure out how to do this with some type of math logic... any suggestions, if possible? Thanks. Link to comment https://forums.phpfreaks.com/topic/105285-not-the-typical-css-alternating-colored-rows-a-little-deeper/ Share on other sites More sharing options...
BlueSkyIS Posted May 12, 2008 Share Posted May 12, 2008 talk it out: 1 blue, 2 white, 1 grey, 2 white, 1 blue, 2 white, etc. Link to comment https://forums.phpfreaks.com/topic/105285-not-the-typical-css-alternating-colored-rows-a-little-deeper/#findComment-539108 Share on other sites More sharing options...
soycharliente Posted May 12, 2008 Share Posted May 12, 2008 <?php $sql = "SELECT * FROM `table`"; $result = mysql_query($sql) OR DIE ("{$sql}<br />".mysql_error()); if (mysql_num_rows($result) > 0) { $c = 0; echo "<table>"; echo "<tr>"; echo "<th>ONE</th><th>TWO</th><th>THREE</th>"; echo "</tr>"; while ($r = mysql_fetch_array($result)) { $c++; $one = $r['one']; $two = $r['two']; $three = $r['three']; // which row are we on? // mod gives back the remainder. $c is counting what row we are on. // you have a cycle of 6 before the pattern repeats. // if $mod == 1, then we are on the first row in the cycle, // if $mod == 5, then we are on the fifth, etc. $mod = ($c % 6); switch($mod) { case 1: $class = "bg_blue"; // if it's row 1, then bg is blue break; case 4: $class = "bg_gray"; // if it's row 4, then bg is gray break; default: $class = "bg_white"; // everything else is white break; } echo "<tr class=\"{$class}\">"; echo "<td>{$one}</td><td>{$two}</td><td>{$three}</td>"; echo "</tr>"; } echo "</table>"; } ?> Link to comment https://forums.phpfreaks.com/topic/105285-not-the-typical-css-alternating-colored-rows-a-little-deeper/#findComment-539138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.