Jump to content

Not the typical CSS Alternating colored rows... a little deeper...


suttercain

Recommended Posts

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.

<?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>";
}
?>

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.