blog

Home / DeveloperSection / Blogs / Pagination in PHP

Pagination in PHP

Anonymous User 4749 15-Nov-2011

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.


php php 
Updated 18-Sep-2014
I am a content writter !

Leave Comment

Comments

Liked By