Jump to content

Alternative row coloring for php pages' tables.


ted_chou12

Recommended Posts

[color=red]Use modulus (%)

<table>
[code]
<?
$i = 1;
$max = 50;

while ($i < $max)
{
if ($i%2 == 0)
{
?>
<tr bgcolor="#aaaaaa"><td>
</td></tr>
<?
}//end if
else
{
?>
<tr bgcolor="#ffffff"><td>
</td></tr>
<?
}//end else
$i++;
}//end while
</table>
[/code]
What modulos does is divides the number you have by the modulos: example $i = 4 (making it 4/2) and takes the remainder (ie. 0). If the modulos (in this case 0) is equal to what we are looking for,background color #aaaaaa, else #ffffff

Hope that made sense[/color]

this code is retained from some other sites, i just have a question to ask:
do you always have to set max &  min of it? because if they are not set the table seems to loop non-stop, but if i keep the max constant, my data keeps changing, wouldn't the last rows look repeated? ???
that $max has nothing to do with alternating row colors. the modulus condition is what alternates it. the $max looks like it is just setting how many rows to actually display.  If you do not set them, you create in infinite loop, because it loops WHILE $i is less than $max.  If you do not set them, the condition will never be false, hence the infinite loop.
make a class to use like this:

[code]
<?php

$a = new alternator;
echo "<table>";
while(/*condition*/)
{
  $color = $a->go();
echo "<tr bgcolor='$color'> ";
echo "<td>Lorem Ipsum sit amet</td>"
echo "</tr>";
}

echo "</table>";
?>

[/code]

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.