Spike121 Posted January 25, 2009 Share Posted January 25, 2009 I'm fairly new to PHP, and I've made a script that outputs each link that is in my database, until there are no more links left. But for my front page, I want just like, 10 links, is there a way to stop the loop after it reaches 10? do { echo "<tr><td valign='top' bgcolor='#".$backcolor."'><center><a href='".$row['Address']."' target='_blank'>".$row['Address']."</a><br><br>".$row['Keywords']."</cemter></td>"; echo "<td valign='top' bgcolor='#".$backcolor."'>".$row['Description']."</td></tr>"; } while($row = mysql_fetch_assoc($result)); And if you can also help on this one (It's not necessary, but it would be cool) I'm trying to make alternating colors for the background color of the cell ($backcolor). Above that script is: if ($backcolor = "33ADFF") { $backcolor="FFFFFF"; } else { $backcolor="33ADFF"; } 1. Should I put it inside the do, while loop, or above it? 2. It doesn't work, but I don't know why. I thought it should. o.o Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/ Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 yes in the sql LIMIT 10 Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/#findComment-745619 Share on other sites More sharing options...
Spike121 Posted January 25, 2009 Author Share Posted January 25, 2009 So... $result = mysql_query("SELECT * FROM sites LIMIT 10") Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/#findComment-745624 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 try it Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/#findComment-745625 Share on other sites More sharing options...
Spike121 Posted January 25, 2009 Author Share Posted January 25, 2009 Perfect, thanks. Can you do anything about the alternating colors though? Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/#findComment-745631 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 So it is like red green red green etc. If so then one way could be $i = 0; while(looping stuff) { if($i == 1){ echo "red"; $i = 0; } elseif($i == 0) {echo "green"; $i = 1; } } sorry bout the code in a rush Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/#findComment-745632 Share on other sites More sharing options...
Spike121 Posted January 25, 2009 Author Share Posted January 25, 2009 $i = 0; do { echo "<tr><td valign='top' bgcolor='#".$backcolor."'><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='#".$backcolor."'>".$row['Description']."</td></tr>"; } while($row = mysql_fetch_assoc($result)); { if($i == 1){ $backcolor="red"; $i = 0; } elseif($i == 0) { $backcolor="green"; $i = 1; } } That's what I did, is that what you mean? I changed it a bit obviously. Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/#findComment-745634 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 Does it work? Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/#findComment-745635 Share on other sites More sharing options...
Spike121 Posted January 25, 2009 Author Share Posted January 25, 2009 No Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/#findComment-745636 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 Hmm, i am not sure why not unless it is something to do with the fact you are using do while loop which isn't needed. Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/#findComment-745639 Share on other sites More sharing options...
Spike121 Posted January 25, 2009 Author Share Posted January 25, 2009 Well, I tried the while loop the first time, but it always skipped the first entry in the database, but when I tried the do while loop, it got the first one aswell, so I stuck with it. Quote Link to comment https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/#findComment-745640 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.