php while loops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

php while loops

Post 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);
}
griffinmt
Forum Commoner
Posts: 35
Joined: Sun Jul 16, 2006 8:37 pm
Location: Michigan

Post 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
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: php while loops

Post 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';
(#10850)
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post 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){ }
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Did you actually try either way mentioned?
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why on earth are you still running an infinite loop?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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!
(#10850)
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post 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.
calmchess
Forum Commoner
Posts: 33
Joined: Fri Aug 18, 2006 8:14 pm

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Insulting a moderator is so not cool :twisted: Just as well it was only arborint :lol: :P

j/k
Post Reply