Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Limit/Stop a Loop

    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?

    Code:
    <? 
    	    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>
        	<? }
        	} ?>

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Change
    PHP Code:
    for($i=0$i mysql_num_rows($result); $i++) 
    To:
    PHP Code:
    for($i=0$i 10$i++) 

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by ptmuldoon View Post
    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:
    Code:
    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:
    PHP Code:
    $num_rows mysql_num_rows($result);
    for(
    $i=0$i $num_rows$i++) 

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Surrey, England
    Posts
    286
    Thanks
    2
    Thanked 2 Times in 2 Posts
    PHP Code:
    <?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.
    Daniel Warner
    - - - - - - - - - -
    GuitarMart.co.uk - Free Online Classified Advertisement for Guitarists in the United Kingdom. Free Ads with a Free Photo.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by danielwarner View Post
    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 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.

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •