Jump to content

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]
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.