Jump to content

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.

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.