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
HormonX
Forum Commoner
Posts: 50 Joined: Tue Dec 10, 2002 7:43 pm
Location: Toronto
Post
by HormonX » Sat Dec 11, 2004 1:25 pm
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.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Dec 11, 2004 1:37 pm
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 » Sat Dec 11, 2004 1:44 pm
Code: Select all
for ($i=0; $i < 10; $i++) {
echo "Row $i - " . (( $i % 2) ? "odd" : "even") . "\n";
}
ol4pr0
Forum Regular
Posts: 926 Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador
Post
by ol4pr0 » Sat Dec 11, 2004 3:13 pm
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 » Sat Dec 11, 2004 3:30 pm
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 » Sat Dec 11, 2004 5:34 pm
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.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Sat Dec 11, 2004 9:49 pm
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 » Sat Dec 11, 2004 9:54 pm
Code: Select all
<?php
$color = ($color == "red") ? "blue" : "red";
?>
LostOne
Forum Newbie
Posts: 18 Joined: Wed Jul 28, 2004 3:21 pm
Location: Florida
Post
by LostOne » Mon Dec 13, 2004 9:47 am
qads, thanks. I have been using Mod all this time......