How to Create Simple Pagination With MySQL Results

When there is the need of showing records on web page from the database, then you need to have pagination on the results for showing proper results. We can not show all the records in the single page. And it is impossible when results are thousands and lakhs. For this kind of need, we need to know how to create pagination with PHP and MySQL.
Here i am going to show how you can show database data on the webpage with pagination.
Suppose there is a table name employee which holds all the records of emplyees of the company. I am going to fetch the records on the webpage.
// Check if start variable is there for paging. if(isset($_GET['start'])) { $start=$_GET['start']; } else { $start=0; } $eu = ($start - 0); $limit = 10; // No of records to be shown per page. $this1 = $eu + $limit; $back = $eu - $limit; $next = $eu + $limit; // Get count of all the records $query2="select * from employee"; $result2=mysql_query($query2); $nume=mysql_num_rows($result2); // The variable nume above will store the total number of records in the table//// $sql="select * from employee limit ".$eu.",".$limit; $result=mysql_query($sql); while ($data=mysql_fetch_array($result)) { echo("<tr><td>$data['name']</td><td>$data['salary']</td><td>$data['grade']</td></tr>") } if($nume > $limit ) { // Let us display bottom links if sufficient records are there for paging /////////////// Start the bottom links with Prev and next link with page numbers ///////////////// echo("<center>"); //// if our variable $back is equal to 0 or more then only we will display the link to move back //////// if($back >=0) { echo("<a href=\"viewrecords.php?start=".$back."\"><font face='Verdana' size='2'>PREV</font></a> "); } //////// Let us display the page links at center. We will not display the current page as a link /////////// $i=0; $l=1; for($i=0;$i < $nume;$i=$i+$limit){ if($i <> $eu){ echo("<a href=\"viewrecords.php?start=".$i."\"> <font face='Verdana' size='2'>".$l."</font></a> "); } else { echo("<font face='Verdana' size='4' color=red>".$l."</font> ");} /// Current page is not displayed as link and given font color red $l=$l+1; } /// If we are not in the last page then Next link will be displayed. Here we check that ///// if($this1 < $nume) { echo("<a href=\"viewrecords.php?start=".$next."\"> <font face='Verdana' size='2'>NEXT</font></a>");} echo("</center>"); }
In case, you have problem in implementing this pagination php code, you can comment and ask the problem.
Leave a comment
Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.