Spike121 Posted January 25, 2009 Share Posted January 25, 2009 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? Link to comment https://forums.phpfreaks.com/topic/142339-solved-help-alternating-colors/ Share on other sites More sharing options...
DarkSuperHero Posted January 25, 2009 Share Posted January 25, 2009 <?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 https://forums.phpfreaks.com/topic/142339-solved-help-alternating-colors/#findComment-745811 Share on other sites More sharing options...
Spike121 Posted January 25, 2009 Author Share Posted January 25, 2009 YES, thank you very much DarkSuperHero Link to comment https://forums.phpfreaks.com/topic/142339-solved-help-alternating-colors/#findComment-745814 Share on other sites More sharing options...
kenrbnsn Posted January 25, 2009 Share Posted January 25, 2009 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 https://forums.phpfreaks.com/topic/142339-solved-help-alternating-colors/#findComment-745822 Share on other sites More sharing options...
DarkSuperHero Posted January 25, 2009 Share Posted January 25, 2009 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.. :-) Link to comment https://forums.phpfreaks.com/topic/142339-solved-help-alternating-colors/#findComment-745874 Share on other sites More sharing options...
Spike121 Posted January 26, 2009 Author Share Posted January 26, 2009 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. Link to comment https://forums.phpfreaks.com/topic/142339-solved-help-alternating-colors/#findComment-746140 Share on other sites More sharing options...
DarkWater Posted January 26, 2009 Share Posted January 26, 2009 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 https://forums.phpfreaks.com/topic/142339-solved-help-alternating-colors/#findComment-746142 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.