SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    display limited records per page

    hi,
    I have code like....

    $sql="select *from students";
    $result=mysql_query($sql,$conn);
    echo "<table>";
    echo "<th>Roll Number</th><th>Name</th>";

    while($row=mysql_fetch_rows($result))
    {
    echo "<tr>";
    echo "<td>$row[0]</td><td>$row[1]</td>";
    echo"</tr>";
    }

    now the table student may contain any number of entries.....My requirement is to display 10 results at a time. The next results will be shown only when i click on next button..
    How should i do it......
    Is it possible to do it with little modifications to above code......
    thanks a lot
    bye and take care

  2. #2
    SitePoint Addict n0other's Avatar
    Join Date
    Feb 2005
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi man_bag. This is achieved with the LIMIT option of mysql. Here's a little example
    Code:
    <?php
    
    // How much entries we will show per page
    $entriesPerPage = 10;
    
    // Check if the GET variable page is set
    if (!isset($_GET['page']) || empty($_GET['page'])) {
        $page = 1;
    } else {
        $page = $_GET['page'];
    }
    
    $startPosition = ($page - 1) * $entriesPerPage;
    
    $sql = 'SELECT * FROM STUDENTS LIMIT '.$startPosition.', '.$entriesPerPage;
    $result = mysql_query($sql, $conn);
    
    // Now you should have right results retrieved, form the links and your done.
    ?>
    Warning: I havent tested it.

  3. #3
    SitePoint Enthusiast ada80ro's Avatar
    Join Date
    Apr 2005
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    link to pagination tutorial

    here is a link to an excelent pagination tutorial. I've use it and it's working great.
    http://www.phpfreaks.com/tutorials/73/1.php


Bookmarks

Posting Permissions

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