PHP Mysql pagination tutorial
We covered how to display mysql data in HTML table in my previous tutorial, now we are going to learn how to display perfect pagination for displaying mysql data in this tutorial. i have written inline comments in code, please go through it and let me know if you have confusion or further improvement in coding or tutorial.
<?php //auther : http://maheshchari.com //connect to mysql server with host,username,password //if connection fails stop further execution and show mysql error $connection=mysql_connect('localhost','root','') or die(mysql_error()); //select a database for given connection //if database selection fails stop further execution and show mysql error mysql_select_db('test',$connection) or die(mysql_error()); //we are displaying 5 results per page $per_page = 5; //number of adjacent links we show left and right of the current page. $adjacents = 5; //execute a mysql query to retrieve count of total users in table //if query fails stop further execution and show mysql error $pages_query = mysql_query("SELECT COUNT('id') FROM users") or die(mysql_error()); //get total number of pages to be shown from total result $pages = ceil(mysql_result($pages_query, 0) / $per_page); //get current page from URL ,if not present set it to 1 $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1 ; //calculate actual start page with respect to Mysql $start = ($page - 1) * $per_page; //execute a mysql query to retrieve all result from current page by using LIMIT keyword in mysql //if query fails stop further execution and show mysql error $query = mysql_query("SELECT * FROM users LIMIT $start, $per_page") or die(mysql_error()); ?> <?php // our main pagination logic goes here //store pagination result in a string so that we can place any where in page. $pagination=""; //if current page is first show first only else reduce 1 by current page $Prev_Page = ($page==1)?1:$page - 1; //if current page is last show last only else add 1 to current page $Next_Page = ($page>=$pages)?$page:$page + 1; //if we are not on first page show first link if($page!=1) $pagination.= '<a href="?page=1">First</a>'; //if we are not on first page show previous link if($page!=1) $pagination.='<a href="?page='.$Prev_Page.'"><code><<</code>Previous</a>'; //we are going to display 5 links on pagination bar $numberoflinks=5; //find the number of links to show on right of current page $upage=ceil(($page)/$numberoflinks)*$numberoflinks; //find the number of links to show on left of current page $lpage=floor(($page)/$numberoflinks)*$numberoflinks; //if number of links on left of current page are zero we start from 1 $lpage=($lpage==0)?1:$lpage; //find the number of links to show on right of current page and make sure it must be less than total number of pages $upage=($lpage==$upage)?$upage+$numberoflinks:$upage; if($upage>$pages)$upage=($pages-1); //start building links from left to right of current page for($x=$lpage; $x<=$upage; $x++){ //if current building link is current page we don't show link,we show as text else we show as linkn $pagination.=($x == $page) ? ' <strong>'.$x.'</strong>' : ' <a href="?page='.$x.'">'.$x.'</a>' ; } //we show next link and last link if user doesn't on last page if($page!=$pages) $pagination.= ' <a href="?page='.$Next_Page.'">Next>></a>'; if($page!=$pages) $pagination.= ' <a href="?page='.$pages.'">Last</a>'; //display final pagination bar. ?> <div class="pagination" style="text-align:center"><?php echo $pagination; ?></div> <table width="100%" border="0"> <tr> <td align="center">Id</td> <td align="center">First Name</td> <td align="center">Last Name</td> <td align="center">Created</td> </tr> <?php //while we going through each row we display info while($row = mysql_fetch_object($query)){ ?> <tr> <td align="center"><?php echo $row->id; //row id ?></td> <td align="center"><?php echo $row->fname; // row first name ?></td> <td align="center"><?php echo $row->lname; //row las tname ?></td> <td align="center"><?php echo $row->created; //row created time ?></td> </tr> <?php } ?> </table> <div class="pagination" style="text-align:center"><?php echo $pagination; ?></div> |

