Jump to content

[SOLVED] [Help] Alternating colors


Spike121

Recommended Posts

Okay, so I've tried this in various forms, but none seem to work. Here's what I've got:

 

if($i == 0){
do {
echo "<tr><td valign='top' bgcolor='#FFFFFF'><center><font color='#000000'><a href='".$row['Address']."' target='_blank'>".$row['Address']."</a></font><br><br>".$row['Keywords']."</center></td>";
echo "<td valign='top' bgcolor='#FFFFFF'>".$row['Description']."</td></tr>";
$i = 1;
} while($row = mysql_fetch_assoc($result));
}
elseif($i == 1){
do {
echo "<tr><td valign='top' bgcolor='#000000'><center><font color='#FFFFFF'><a href='".$row['Address']."' target='_blank'>".$row['Address']."</a></font><br><br>".$row['Keywords']."</center></td>";
echo "<td valign='top' bgcolor='#000000'>".$row['Description']."</td></tr>";
$i = 0;
} while($row = mysql_fetch_assoc($result));
}

 

Explanation: (What I figure, which hopefully is right)

If $i = 0 (which was set as 0 above that)

then do the two lines of echo with a black background and white text and change $i to 1.

 

Else if $i = 1 (which should be set to 1 after the first if right?) then display the next mysql result, with a background of black, and font of white, and change $i back to 0.

 

So if you don't get what I'm trying to do, I grabbed 10 results from my database, and put it into a table, but I want each table row to be alternating colors (black background, white text then white background, black text.)

 

Unfortunately, I tried that and it doesn't work. I also tried two other ways, which I lost when I remade this one, but neither of those ways worked either. I'm really confused, and annoyed. Can anyone help please? :P

Link to comment
https://forums.phpfreaks.com/topic/142339-solved-help-alternating-colors/
Share on other sites

<?php
$i=0;
do {
if($i == 0){

echo "<tr><td valign='top' bgcolor='#FFFFFF'><center><font color='#000000'><a href='".$row['Address']."' target='_blank'>".$row['Address']."</a></font><br><br>".$row['Keywords']."</center></td>";
echo "<td valign='top' bgcolor='#FFFFFF'>".$row['Description']."</td></tr>";
$i = 1;

}elseif($i == 1){

echo "<tr><td valign='top' bgcolor='#000000'><center><font color='#FFFFFF'><a href='".$row['Address']."' target='_blank'>".$row['Address']."</a></font><br><br>".$row['Keywords']."</center></td>";
echo "<td valign='top' bgcolor='#000000'>".$row['Description']."</td></tr>";
$i = 0;
} 
}while($row = mysql_fetch_assoc($result));

 

try that...its untested...

Here's a much easier way:

<?php
$fcolor = "#FFFFFF";
$bgcolor = "#000000";
while ($row = mysql_fetch_assoc($result)) {
     echo "<tr><td valign='top' bgcolor='$bgcolor'><center><font color='$fcolor'><a href='".$row['Address']."' target='_blank'>".$row['Address']."</a></font><br><br>".$row['Keywords']."</center></td>";
     echo "<td valign='top' bgcolor='$bgcolor'>".$row['Description']."</td></tr>";
     $fcolor = ($fcolor =="#000000")?"#FFFFFF":"#000000";
     $bgcolor = ($bgcolor == "#000000")?"#FFFFFF":"#000000";
}?>

 

Ken

sweet....yeah i agree, kenrbnsn is a "tidyer" and shorter way, although i wanted to use the code that Spike had presented....he was on the right track, but logic was a bit off...been through that so many times...more than i'd like...ahahahaha.. :-)

I prefer codes I can understand, theres some of that new code I don't get. Plus, I'd have to change it to do while, because the while one seems to skip the first entry in my database for some reason, I don't know why.

 

That means that you're calling mysql_fetch_assoc() or some other fetching functions at some point before the while loop.

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.