using for loop to alternate 2 colors in table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HormonX
Forum Commoner
Posts: 50
Joined: Tue Dec 10, 2002 7:43 pm
Location: Toronto

using for loop to alternate 2 colors in table

Post by HormonX »

Can anyone help with this. What i want to do is to alternate 2 different colors in table that will be displaying information from the database.

can i use for loop to that for me ?

Am sure you know what i mean but just in case :)

Row 1 - red
Row2 - blue
Row3 - red
Row4 blue
.. and so on.

Thank you.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php

echo '<table>';

$x = 0;

while ($row = mysql_fetch_assoc($result))
{
$x++;

if ($x % 2 == 1
? $bg = '#000000'
: $bg = '#FFFFFF'
);

echo '<tr><td bgcolor="'.$bg.'">'.$row['rownamehere'].'</td></tr>';

}

echo '</table>';

?>
THis is generally how it is done.
Selkirk
Forum Commoner
Posts: 41
Joined: Sat Aug 23, 2003 10:55 am
Location: Michigan

Post by Selkirk »

Code: Select all

for ($i=0; $i < 10; $i++) {
    echo "Row $i - " . (( $i % 2) ? "odd" : "even") . "\n";
}
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Same thing just a bit differant

Code: Select all

<?
$coloralternator = "";
while($row = mysql_fetch_assoc($result)) {
$color = ($coloralternator++ %2 ? "red" : "blue");
?>
<TR bgcolor="#<?echo $color;?>" #....ect
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

and if you want to support more than 2 colors

Code: Select all

<?php

$colors = array(
    'red',
    'green',
    'blue',
    'yellow',
    'grey',
);

$num_colors = count($colors);

$i = 0;
while ($row = mysql_fetch_assoc($result)) {
    $bgcolor = $colors[$i++ % $num_colors];
    echo "$bgcolor<br>";
}


?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

conclusion:
-use a counter which counts from 1 to number_of_colors and then starts at 1 again

- or be lazy, just add 1 all the time to your counter, and then use modulo function (%) to find out which color you are displaying now.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Yet another way :)

Code: Select all

while($row = mysql_fetch_assoc($result))
{
    $row_bool = !$row_bool;
    $row_class = ($row_bool) ? 'light_row' : 'dark_row'

    echo "<td class = '$row_class'>......";
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

:lol:

Code: Select all

<?php
$color = ($color == "red") ? "blue" : "red";
?>
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

If you don't mind using javascript, two useful links... (some people may have it disabled)


http://www.alistapart.com/articles/tableruler/
http://www.alistapart.com/articles/zebratables/

Regards
LostOne
Forum Newbie
Posts: 18
Joined: Wed Jul 28, 2004 3:21 pm
Location: Florida

Post by LostOne »

qads, thanks. I have been using Mod all this time...... :roll:
Post Reply