Home > DeveloperSection > Blogs > Pagination in PHP

Pagination in PHP


PHP PHP 
Ratings:
0 Comment(s)
 2399  View(s)
Rate this:

Pagination in PHP

As your database grows, showing all the results of a query on a single page is no longer practical that’s why pagination is used in PHP. You can display your results over a number of pages, each linked to the next, and to allow your users to browse your content in bite sized pieces.

Code:

<?php

        // connect with database

          mysql_connect("localhost", "root", "root");

       // select database name

        mysql_select_db("tempdatabase");

        /* We will bypass the database connection code ... */

        // select all data from your table

        $sqlQuery = "select count(*) from tempdatabase.tbltemp";

        // store result of query

        $result = mysql_query($sqlQuery);

        $totalRows = mysql_num_rows($result);

        $pager_options = array(

        'mode'       => 'Sliding',

        'perPage'    => 10,

        'delta'      => 4,

        'totalItems' => $totalRows,

        );

        $pager = Pager::factory($pager_options);

        // return the links of pages

        echo $pager->links;

?>

The above code will return the following pagination links.

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 » [10]

Assuming the page where the above code is included is named ‘MydataPage.php’; the pager links will have a url like follows:

MydataPage.php?pageID=n

Where n is the page number.


Don't want to miss updates? Please click the below button!

Follow MindStick