For/While Loop

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

Post Reply
kerepuki
Forum Commoner
Posts: 30
Joined: Fri Oct 29, 2004 12:28 am

For/While Loop

Post by kerepuki »

Hey guys, first post here in a long time. I am doing quite a bit of coding lately and have come across a small (and hopefully easy) problem.

I have a for loop (for($i = 0; $i <= 5; $i++) etc) and inside this for loop I have a while/fetch array statement.

My problem is that my for loop is ignored. Why would this be?

Thanks in advance.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: For/While Loop

Post by Christopher »

You would need to post some code.
(#10850)
kerepuki
Forum Commoner
Posts: 30
Joined: Fri Oct 29, 2004 12:28 am

Re: For/While Loop

Post by kerepuki »

***** PLEASE USE THE

Code: Select all

TAG WHEN POSTING ******[/color]

Code: Select all

<?php
                        
for($c = 0; $c <= 5; $c++){
                            
while($data_row_c = mysql_fetch_array($data_query_c)){
                                
$item_row_c = mysql_fetch_array($item_query_c);
                                
if($active_row_c['confirmed'] == 1){
                        
?>
 
<!--THEN MY CODE TO DISPLAY RESULTS-->
 
<?php
 
}
 
}
 
}
 
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: For/While Loop

Post by califdon »

kerepuki wrote:***** PLEASE USE THE TAG WHEN POSTING ******

Code: Select all

<?php
for($c = 0; $c <= 5; $c++){
while($data_row_c = mysql_fetch_array($data_query_c)){
$item_row_c = mysql_fetch_array($item_query_c);
if($active_row_c['confirmed'] == 1){
?>
 
<!--THEN MY CODE TO DISPLAY RESULTS-->
 
<?php
}
}
}
?>
I'd say either no rows were returned by your query (did you test it to see?) or the $active_row_c['confirmed'] doesn't equal 1.
kerepuki
Forum Commoner
Posts: 30
Joined: Fri Oct 29, 2004 12:28 am

Re: For/While Loop

Post by kerepuki »

Thanks for the reply, yes I can see my results and the correct results are being displayed however, there are too many of them. I dont want to create a pagination scenario so I thought that it would be ok to create a for loop to only display 5 rows.

It doesnt matter too much, I can just recreate the loop another way if its going to take too much time to solve.

Just seems weird that it wont only display 5 results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: For/While Loop

Post by Christopher »

You really don't need to for loop. Just LIMIT the number of results being returned by the query and use the while loop.
(#10850)
Post Reply