Jump to content

[Help] While loop that stops


Spike121

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/142306-help-while-loop-that-stops/
Share on other sites

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

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.