...

View Full Version : Limit/Stop a Loop



ptmuldoon
03-04-2008, 11:59 PM
I'm looping through a list of players, and checking an 'active' field for each player. And then I want to limit/stop the loop to give me the the first 10 rows.

Unfortunately, I can't simply limit the query, as the 'active' field is not a stored field in the database, but being added to the array later on.

Currently, the code below is giving me all the players with 1 or more active games, when I'd like to limit it 10. I'm not sure, but maybe change the if statement to a do.while loop?



<?
for($i=0; $i < mysql_num_rows($result); $i++){
if($stats[$i]['active'] > 1) {
?>
<tr class="<?php if ($rownum&#37;2) { echo "line_2"; } ?>">
<td>
<?php echo number_format($stats[$i]['Rating'], 3); ?>
</td>
<td style="text-align:left;">
<a href="index.php?page=profile&pid=<?php echo ($stat['id']); ?>"> <?php echo $stats[$i]['login']; ?></a>
</td>
<td>
<?php echo number_format($stats[$i]['active']); ?>
</td>
</tr>
<? }
} ?>

Inigoesdr
03-05-2008, 12:51 AM
Change

for($i=0; $i < mysql_num_rows($result); $i++)
To:

for($i=0; $i < 10; $i++)

ptmuldoon
03-05-2008, 01:11 AM
But that will only check the first 10 rows to see if that player has an active game of 1 or greater.

I need to check the entire array first, and then have it give me the first 10 rows that have active game of 1 or more.

Inigoesdr
03-05-2008, 01:18 AM
I need to check the entire array first, and then have it give me the first 10 rows that have active game of 1 or more.

Then you'll have to make a separate array of the active game rows, and then slice the first 10 from it. Or use another query to the database.

By the way, this condition will be executed each time the loop is iterated:

for($i=0; $i < mysql_num_rows($result); $i++)
And that will call mysql_num_rows() each time, which will slow it down quite a bit if you have a lot of rows. So you should get the number of rows in a variable, and use it instead:

$num_rows = mysql_num_rows($result);
for($i=0; $i < $num_rows; $i++)

danielwarner
03-05-2008, 02:17 AM
<?php

$i = 0; //Array ID
$j = 0; //Count to 10
$total = mysql_num_rows($result);

while($j < 10){

$i++;
if($i <= $total){

if($stats[$i]['active'] > 1) {

$j++;

?>
<tr class="<?php if ($rownum&#37;2) { echo "line_2"; } ?>">
<td>
<?php echo number_format($stats[$i]['Rating'], 3); ?>
</td>
<td style="text-align:left;">
<a href="index.php?page=profile&pid=<?php echo ($stat['id']); ?>"> <?php echo $stats[$i]['login']; ?></a>
</td>
<td>
<?php echo number_format($stats[$i]['active']); ?>
</td>
</tr>
<?php
}
}
}
?>
one possible work around i suppose

untested and off the top of my head though.

Inigoesdr
03-05-2008, 02:24 AM
one possible work around i suppose

untested and off the top of my head though.
With that code if there were less than 10 active rows for any reason the code would enter an infinite loop. I was going to suggest before that the OP keep a count in a similar way, but with the for() loop, and break (http://php.net/break) out of it once 10 rows were reached. But they gave me the impression that breaking out of the loop would affect some code further down.

ptmuldoon
03-05-2008, 01:43 PM
I took Inigoesdr's approach last night, and just made separate array of just those rows with a >= 1 active, and then just looped that array 10 times.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum