Do you want to be a good Web Developer?

Enter Your Email To Get Our Exclusive Tutorials Right In Your Email

Join TalkersCode Community And Recieve Exclusive Tutorials
On Web Development In Your Email

Simple And Best Pagination With PHP, MySQL

Monday, Jan 2, 2017

Tags:- PHP MySQL


Pagination technique is used to fetch small amount data stored in a database inspite of fetching all the data at once from the database. It is very usefull technique it load the page fast. You can make Simple and Best Pagination in just 3 Steps.You may also like ajax pagination using PHP and MySQL.


simple and best pagination
  1. Connect To Database.
  2. Query The MySQL Database.
  3. Get The Results.


Step 1.Connect To Database

In first step we have to connect to the database from which you want to get the data. You can connect to any database from which you want to get the data. In this we use sample database named " demo ".

<?php
$host = 'localhost';
$user = 'root';
$pass = '';

mysql_connect($host, $user, $pass);

mysql_select_db('demo');
?>


Step 2.Query The MySQL Database.

<?php
$offset = 0;
$page_result = 5; 
	
if($_GET['pageno'])
{
 $page_value = $_GET['pageno'];
 if($page_value > 1)
 {	
  $offset = ($page_value - 1) * $page_result;
 }
}

$select_results = " select * from student_info limit $offset, $page_result ";
?>

In second step we use three php variables $offset, $page_result, $page_value. $offset = 0 because we want to select the data from 0 positionfrom the table, $page_result = 5 because we want to display 5 results at a time, $page_value = $_GET['pageno'] it is the value of the page when the user wants to change the page and click the pagination page number. Then if the $page_value is greater than 1 we change the $offset value to the value of this code ($page_value - 1) * $page_result. Then we query the database we select details from student_info table and limit the results returned by the query for more details of limit clause visit our MySQL clause tutorial.You may also like load more results from database using ajax.



Step 3.Get The Results.

<?php

$result = mysql_query( $select_results );

while($row = mysql_fetch_array($result))
{
 echo $row[' student_name '];
 echo $row[' student_rollno '];
 echo $row[' student_course '];
}

$pagecount = 50; // Total number of rows
$num = $pagecount / $page_result ;

if($_GET['pageno'] > 1)
{
 echo "<a href = 'samepage.php?pageno = ".($_GET['pageno'] - 1)." '> Prev </a>";
}
for($i = 1 ; $i <= $num ; $i++)
{
 echo "<a href = "samepage.php?pageno = ". $i ." >". $i ."</a>";
}
if($num! = 1)
{
 echo "<a href = 'samepage.php?pageno = ".($_GET['pageno'] + 1)." '> Next </a>";
}

?>

In this step we get the results from student_info table, in student_info table we have three columns student_name, student_rollno, student_course. then, we use $pagecount = 50 because there are 50 rows in student_info table. Then, we use $num variable and set the value returned by $pagecount / $page_result. The first if statement display the Prev page link if the pageno is greater than 1 and then we display all the pagination links using for loop upto the $num value. The second if statement display the Next page link if the pageno is not equal to 1. Remember always validate data before and after submitting the form.



That's all, this is simple and best way to make Pagination with the help of PHP and MySQL. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email