Adding Pagination in PHP

Here is an example of  how to use pagination in your PHP oage. I’m using a pagination class developed by Shiege Iseng’ and can be downloaded from here.

We include this file in the page we use. So here is a basic script that shows the details of users from a database and shows 4 of them in a pagae and shows 3 pages as links in between the [prev] and [next] links.


//making the database connection

//including the paging class

//database query
 $qry = "SELECT * FROM USERS";
 $results = mysql_query($qry);
 $num_rows = mysql_num_rows($results);

//making a paging object that shows 4 results and 3 links
$paging = new paging(4,3);

 <title>Pagination Example</title>


 <td>Total Results Found: <?php echo $num_rows; //prints total results ?></td>
 <td>Sl No</td>
 <td>First Name</td>
 <td>Last Name</td>

 $sl = 1;
 $pageno = 1;
 $pageno = $pageno - 1;
 if($pageno > 0)
 $s1 = $pageno * 10;
 $s1 = 0;

 if($num_rows > 0)
 $i = 1;
 $s1 = $s1 + 1;
//to alternate b/w colors in the row
 if(($i + 1) % 2 == 0)

 <tr bgcolor="<?php echo $bgcolor?>">
 <td><?php echo $sl; ?></td>
 <td><?php echo $obj['firstname']; ?></td>
 <td><?php echo $obj['lastname']; ?></td>
 <td><?php echo $obj['sex']; ?></td>
 <td><?php echo $obj['dob']; ?></td>
 <td><?php echo $obj['email']; ?></td>


 <td>No Data Available !! Please redifine your Search.... </td>

 <td><?php echo "<hr>".$paging->print_link();?></td>



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s