Page 1 of 1

php while loops

Posted: Sun Aug 20, 2006 8:04 pm
by calmchess
hi everybody.....how do i make the following code loop 5 times ...i've tried various while loops but can't make it do anything except loop indefinately..

Code: Select all

$link = mysql_connect("localhost","root","KKT1KKT1");
mysql_select_db("brimelow_store");

$query = 'select * from products';
$results = mysql_query($query);

echo"<?xml version=\"1.0\"?>\n";
echo"<products>\n";

while($line = mysql_fetch_assoc($results)) {  
echo "<item>" . $line["products"] . "</item>\n";
echo"</products>\n";


mysql_close($link);
}

Posted: Sun Aug 20, 2006 8:13 pm
by griffinmt
Propably the most trivial way to get started is with a for loop, as;

Code: Select all

for ($i=0; $i<5; $i++)
{
     //code to run in loop
}

Re: php while loops

Posted: Sun Aug 20, 2006 8:17 pm
by Christopher
I am guessing that you don't mean the whole code and that you just want to limit it to 5 loops. Probably than controlling your look would be to use the MySQL LIMIT keyword in your SELECT query.

Code: Select all

$query = 'SELECT * FROM products LIMIT 5';

Posted: Sun Aug 20, 2006 9:06 pm
by calmchess
neither of the above two replys answered my question....i'm including the output of the script so you can see what i want to repeat 5 times.....beachball lawnchair rubber boat speed boat fishing pole.....the script works fine with a single query or by looping it infinate times using ......while(1){ }

Posted: Mon Aug 21, 2006 12:26 am
by Christopher
Did you actually try either way mentioned?

Posted: Mon Aug 21, 2006 12:28 am
by feyd
Why on earth are you still running an infinite loop?

Posted: Mon Aug 21, 2006 12:44 am
by Christopher
And just looked at the code again and you have your mysql_close() inside the loop so you are closing your connection after the first fetch!

Posted: Mon Aug 21, 2006 11:03 am
by calmchess
first of all yes i tried both ways you asked me to try....i probably have more computer technology education and know how than you......second it does it really matter if i put the } before mysql_close; since when it loops back to the top it reopens the connection?.......all I want is for the code to loop 5 times instead of infinate times....plz stop asking arbitrary questions and admit that you don't know how to make it loop 5 times.....i don't want my post filled with a bunch of crap.

Posted: Mon Aug 21, 2006 11:11 am
by calmchess
Hold on I'm truly sorry for my last post..............the first explanation worked this morning....i must have had an error that i didn't catch yesterday.....plz forgive me for my php green thumb.

Posted: Mon Aug 21, 2006 12:31 pm
by Weirdan
Image

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.3 wrote:13. A wide variety of opinions may be expressed on this public discussion forum. Respect positions which differ from your own. We like to see a vigorous intellectual discussion which proceeds politely and addresses the technical merits of different positions. Do not expect that anyone will alter their opinion of a topic regardless of however logical you believe your argument to be. Do not attack anyone personally for whatever reason - doing so contravenes the spirit in which this forum was founded and can have serious consequences.

Posted: Mon Aug 21, 2006 3:55 pm
by Chris Corbyn
Insulting a moderator is so not cool :twisted: Just as well it was only arborint :lol: :P

j/k