Results 1 to 4 of 4

Thread: Paging using PHP

  1. #1
    Join Date
    Feb 2009
    Posts
    66

    Paging using PHP

    hello friends,

    Is it possible to perform paging using PHP scripts and codes ? If possible, then can anybody help me providing necessary information about the same ? thanks...

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Paging using PHP

    Paging means showing your query result in multiple pages instead of just put them all in one long page. Imagine waiting for five minutes just to load a search page that shows 1000 result. By splitting the result in multiple pages you can save download time plus you don't have much scrolling to do.

    To show the result of a query in several pages first you need to know how many rows you have and how many rows per page you want to show. For example if I have 792 rows and I show 40 rows per page that mean I'll have 20 pages (rounded up).

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Paging using PHP

    Here the example php code for paging....
    I created a table named randoms that store 295 random numbers. Each page shows 20 numbers.

    Code:
    <?php
    include 'library/config.php';
    include 'library/opendb.php';
    
    // how many rows to show per page
    $rowsPerPage = 20;
    
    // by default we show first page
    $pageNum = 1;
    
    // if $_GET['page'] defined, use it as page number
    if(isset($_GET['page']))
    {
        $pageNum = $_GET['page'];
    }
    
    // counting the offset
    $offset = ($pageNum - 1) * $rowsPerPage;
    
    $query = " SELECT val FROM randoms " .
             " LIMIT $offset, $rowsPerPage";
    $result = mysql_query($query) or die('Error, query failed');
    
    // print the random numbers
    while($row = mysql_fetch_array($result))
    {
       echo $row['val'] . '<br>';
    }
    
    // ... more code here
    ?>

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Paging using PHP

    MySQL helps to generate paging by using LIMIT clause which will take two arguments. First argument as OFFSET and second argument how many records should be returned from the database.

    Below is a simple example to fetch records using LIMIT clause to generate paging.

    Example: Try out following example to display 10 records per page.

    Code:
    <html>
    <head>
    <title>Paging Using PHP</title>
    </head>
    <body>
    <?php
    $dbhost = 'localhost:3036';
    $dbuser = 'root';
    $dbpass = 'rootpassword';
    $rec_limit = 10;
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('test_db');
    /* Get total number of records */
    $sql = "SELECT count(emp_id) FROM employee ";
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    $row = mysql_fetch_array($retval, MYSQL_NUM );
    $rec_count = $row[0];
    
    if( isset($_GET{'page'} ) )
    {
       $page = $_GET{'page'} + 1;
       $offset = $rec_limit * $page ;
    }
    else
    {
       $page = 0;
       $offset = 0;
    }
    $left_rec = $rec_count - ($page * $rec_limit);
    
    $sql = "SELECT emp_id, emp_name, emp_salary ".
           "FROM employee ".
           "LIMIT $offset, $rec_limit";
    
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
        echo "EMP ID :{$row['emp_id']}  <br> ".
             "EMP NAME : {$row['emp_name']} <br> ".
             "EMP SALARY : {$row['emp_salary']} <br> ".
             "--------------------------------<br>";
    } 
    
    if( $page > 0 )
    {
       $last = $page - 2;
       echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a> |";
       echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
    }
    else if( $page == 0 )
    {
       echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
    }
    else if( $left_rec < $rec_limit )
    {
       $last = $page - 2;
       echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a>";
    }
    mysql_close($conn);
    ?>

Similar Threads

  1. How to defragment the paging file
    By SoftWore in forum Tips & Tweaks
    Replies: 1
    Last Post: 31-08-2010, 07:38 PM
  2. Reading pdf on iPad and paging down
    By Menominee in forum Portable Devices
    Replies: 6
    Last Post: 25-07-2010, 12:55 AM
  3. Need ASP paging navigation help!
    By VijayW in forum Software Development
    Replies: 3
    Last Post: 25-02-2009, 06:42 PM
  4. Paging consumption
    By Arlo in forum Operating Systems
    Replies: 6
    Last Post: 08-10-2008, 05:59 PM
  5. Paging Results
    By Waffle in forum Software Development
    Replies: 3
    Last Post: 06-10-2008, 04:10 PM

Tags for this Thread

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
  •  
Page generated in 1,713,452,520.48368 seconds with 17 queries