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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.