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 4 of 4

Thread: loop help

  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    loop help

    I have the below code that is pulling all rows out of table and then listing them in a table. But I only want it to show the first 10 rows only, and not sure how to cap it at 10.

    I'm currently using a while loop as follows:
    PHP Code:
    <?php
                
    while ($stat mysql_fetch_array($result))
                
                { 
    ?>
                <tr class="<?php if ($rownum%2) { echo "line_2"; } ?>">
                    <td>
                        <?php echo number_format(($stat['Rating']), 3); ?>
                    </td>
                    <td style="text-align:left;">
                    <a href="index.php?page=profile&pid=<?php echo ($stat['id']); ?>"> <?php echo ($stat['login']); ?></a>
                    </td>
                </tr>
            <?php
                
    echo "\n";
                } 
            
    ?>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Include "LIMIT 10" in your query (last line), or use a counter in your loop (a FOR loop works well for this).

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Use LIMIT in your mysql query

    http://php.about.com/od/mysqlcommands/g/Limit_sql.htm

    (not best site, was first on google :s )

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Thanks

    Work perfect. I was struggling to get a For loop to work with, and get getting all the rows. But the LIMIT works just as good.


  •  

    Posting Permissions

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