PHP MySQL Pagination – Tutorialpath

Welcome to all our readers on our website at www.tutorialpath.com. As you all know that here on our website we will provide you lots of information related to the latest and newly introduced technology which helps you to get updated with the latest tech. Here you will get all the useful information which is required in this tech world. As you know technology is one of the trending parts of this world. So, here again, we came up with the latest and new tech information which is related to the process of creating PHP MySQL Pagination. With the help of this article, you will get to know the complete process of how to create a PHP MySQL Pagination function in a step by step manner. So, simply have a look at this article and grab all the useful information which is going to be very helpful for you.

PHP MySQL Pagination

PHP MySQL Pagination
PHP MySQL Pagination

 

MySQL select queries can display thousands of records from a database. It is not possible to show thousands of these records on one page. For that purpose, we use the concept of MySQL pagination. In MySQL Pagination, we divide the results into several pages. Basically it is a method of showing slightly fewer results on a page than putting them all together on the same page. Suppose a table contains 15 records. 5 records are displayed on each page using pagination.

There will be 5 records in every 3 pages. Each time we click on the next page the next 5 records will be displayed and the previous 5 records will be removed. The example is below. In the example above, there is a Paget function that has two arguments, one is the connection to the database, and the other is the limit variable which is basically the number of records on each page. The FOUND_ROWS () function returns the number of rows from the table in the absence of a boundary block.

Show 5 records on each page

There are 15 records inside the user information table. Show only 5 records on each page. Save As (Use Pagination)

paginate.php

<?php

function paginage($db,$limit)
{

$sql ='select FOUND_ROWS();';

$result=mysql_query($sql);

$row=mysql_fetch_array($result);

$numrows=$row[0];

$paglinks='<p>';

if($numrows > $limit)

{

$page=$_GET['page']?$_GET['page']:1; 

//set page click_page
$currentpage=$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];

$currentpage=str_replace('&page='.$page,'',$currentpage);

//now if page_click on 1
if($page==1)

{

$paglinks.='<PREV';

}

else
{

$page_pre=$page-1;

$paglinks.='<a href="'.$currentpage.'&page='.$page_pre.'
"><PREV</a>';

}

//number of pages and range(no of links show)
$numofpages=ceil($numrows/$limit);

$range=4;

//left range and right range
$l_range=max(1,$page-(($range-1)/2));

$r_range=min($numofpages,$page+(($range-1)/2));

if(($r_range-$l_range)< $range-1)

{

if($l_range==1)

{

$r_range=min($l_range + ($range-1),$numofpages);

}

else

{

$l_range=max($r_range-($range-1),0);

}

}

$paglinks.=($l_range>1)?'....':'  ';

for($i=1; $i<$numofpages; $i++)//for loop this make the numric page link show

{

if($i==$page)

{

$paglinks.=$i;

}

else

{

if($l_range<= $i and $i<=$r_range)

{

$paglinks.='<a href="'.$currentpage.'&page='.$i.'">['.$i.']</a>';

}

}

}

$paglinks.=($r_range< $numofpages)?'....':'  ';

if($numrows-($limit * $page)>0)

{

$pagenext=$page +1;

$paglinks .=	'<a href="'.$currentpage.'&page='.$pagenext.'">  NEXT></a>';

}

else

{

$paglinks .='NEXT>';

}

}

else

{

$paglinks.='<PRVE   NEXT> ';

}

$paglinks.='</p>';

return $paglinks;

}		 

?>

Select the record from the user information table (total records are 12). Show 5 records on each page.

<?php

include("paginate.php");

$conn=mysql_connect("localhost","root");

$db=mysql_select_db("test",$conn);

error_reporting(1);

?>

<body>

<table border="1"  class="list" cellpadding="4px" >

<tr>

<td>SN</td>

<td>Name</td>

<td>Email</td>

<td>Mobile</td>

</tr>

<?php

$page=$_GET['page']?$_GET['page']:1;

$limit=5;

$start=($page-1)*$limit; 

$query4='select SQL_CALC_FOUND_ROWS * from userinfo limit '.$start.','.$limit;

$result=mysql_query($query4);

$page_links=paginage($db,$limit);

$num=1;

while($row=mysql_fetch_array($result))
{

echo'<tr>';

echo'<td>'.$row['id'].'</td>';

echo'<td>'.$row['name'].'</td>';

echo'<td>'.$row['email'].'</td>';

echo'<td>'.$row['mobile'].'</td>';

$num++;

echo'</tr>';

}

?>

<tr>

<td colspan="6" align="center"><?php echo $page_links; ?></td>

</tr>

</table>					

</body>

</html>

In Display.php we have included paginate.php and the results obtained by it are shown in tabular form. The records in the result variable are obtained by the mysql_fetch_array () function which is placed in a while loop to display all the records. The information obtained by the display function is divided into pages and each page has a maximum of 5 records.

Leave a Reply

Your email address will not be published. Required fields are marked *