Pagination in php is very simple concepts. You can learn easily. Just follow the below steps.
1. Find the Start values based on page number:
$limit=10;
$page=$_GET['p'];
if($page=='')
{
$page=1;
$start=0;
}
else
{
$start=$limit*($page-1);
}
Where,
$limit is the number rows per page.
$page is page number.
$start is starting point of limit in mysql query.
If the page number is 1, then the start is 0 which is find by $start=$limit*($page-1).
$start=10*(1-1)
$start=10*(0)
$start=0
If the page number is 2, then the start is 10.
Similarly, if the page number is 3, then the start is 20.
2. Find total number of records in mysql table:
Then we need to calculate the total number of data in table. Then find the maximum pages using php script like below:
$tot=mysql_query("SELECT * FROM table1") or die(mysql_error());
$total=mysql_num_rows($tot);
$num_page=ceil($total/$limit);
Where,
mysql_num_rows() returns the numbers results in numeric.
ceil() returns the whole digit number. ie, ceil(2.3) => 3.
$maxpage returns the number of pages.
3. Function for pagination:
The php script for pagination function is:
function pagination($page,$num_page)
{
echo'<ul style="list-style-type:none;">';
for($i=1;$i<=$num_page;$i++)
{
if($i==$page)
{
echo'<li style="float:left;padding:5px;">'.$i.'</li>';
}
else
{
echo'<li style="float:left;padding:5px;"><a href="pagination.php?p='.$i.'">'.$i.'</a></li>';
}
}
echo'</ul>';
}
Where,
You need to use for loop for display number of pages in mysql table.
4.The whole php code for pagination as follows as:
Now you'll get output like this:
Now you can paginated to next page.
Related Post:
1. Find the Start values based on page number:
$limit=10;
$page=$_GET['p'];
if($page=='')
{
$page=1;
$start=0;
}
else
{
$start=$limit*($page-1);
}
Where,
$limit is the number rows per page.
$page is page number.
$start is starting point of limit in mysql query.
If the page number is 1, then the start is 0 which is find by $start=$limit*($page-1).
$start=10*(1-1)
$start=10*(0)
$start=0
If the page number is 2, then the start is 10.
Similarly, if the page number is 3, then the start is 20.
2. Find total number of records in mysql table:
Then we need to calculate the total number of data in table. Then find the maximum pages using php script like below:
$tot=mysql_query("SELECT * FROM table1") or die(mysql_error());
$total=mysql_num_rows($tot);
$num_page=ceil($total/$limit);
Where,
mysql_num_rows() returns the numbers results in numeric.
ceil() returns the whole digit number. ie, ceil(2.3) => 3.
$maxpage returns the number of pages.
3. Function for pagination:
The php script for pagination function is:
function pagination($page,$num_page)
{
echo'<ul style="list-style-type:none;">';
for($i=1;$i<=$num_page;$i++)
{
if($i==$page)
{
echo'<li style="float:left;padding:5px;">'.$i.'</li>';
}
else
{
echo'<li style="float:left;padding:5px;"><a href="pagination.php?p='.$i.'">'.$i.'</a></li>';
}
}
echo'</ul>';
}
Where,
You need to use for loop for display number of pages in mysql table.
4.The whole php code for pagination as follows as:
<?php
error_reporting('E_ALL ^ E_NOTICE');
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$page=$_REQUEST['p'];
$limit=10;
if($page=='')
{
$page=1;
$start=0;
}
else
{
$start=$limit*($page-1);
}
$query=mysql_query("select * from table1 limit $start, $limit") or die(mysql_error());
$tot=mysql_query("select * from table1") or die(mysql_error());
$total=mysql_num_rows($tot);
$num_page=ceil($total/$limit);
echo'<table><th>Reg.Id</th><th>Name</th><th>Category</th>';
while($res=mysql_fetch_array($query))
{
echo'<tr><td>'.$res['game_ID'].'</td><td>'.$res['game_Title'].'</td><td>'.$res['category_Name'].'</td></tr>';
}
echo'</table>';
function pagination($page,$num_page)
{
echo'<ul style="list-style-type:none;">';
for($i=1;$i<=$num_page;$i++)
{
if($i==$page)
{
echo'<li style="float:left;padding:5px;">'.$i.'</li>';
}
else
{
echo'<li style="float:left;padding:5px;"><a href="pagination.php?p='.$i.'">'.$i.'</a></li>';
}
}
echo'</ul>';
}
if($num_page>1)
{
pagination($page,$num_page);
}
?>
error_reporting('E_ALL ^ E_NOTICE');
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$page=$_REQUEST['p'];
$limit=10;
if($page=='')
{
$page=1;
$start=0;
}
else
{
$start=$limit*($page-1);
}
$query=mysql_query("select * from table1 limit $start, $limit") or die(mysql_error());
$tot=mysql_query("select * from table1") or die(mysql_error());
$total=mysql_num_rows($tot);
$num_page=ceil($total/$limit);
echo'<table><th>Reg.Id</th><th>Name</th><th>Category</th>';
while($res=mysql_fetch_array($query))
{
echo'<tr><td>'.$res['game_ID'].'</td><td>'.$res['game_Title'].'</td><td>'.$res['category_Name'].'</td></tr>';
}
echo'</table>';
function pagination($page,$num_page)
{
echo'<ul style="list-style-type:none;">';
for($i=1;$i<=$num_page;$i++)
{
if($i==$page)
{
echo'<li style="float:left;padding:5px;">'.$i.'</li>';
}
else
{
echo'<li style="float:left;padding:5px;"><a href="pagination.php?p='.$i.'">'.$i.'</a></li>';
}
}
echo'</ul>';
}
if($num_page>1)
{
pagination($page,$num_page);
}
?>
Now you'll get output like this:
Now you can paginated to next page.
Related Post:
This is very useful to me... Actually i got more trouble in pagination concepts. But it is very clear and simple to unterstand. Thank you very for this post...
ReplyDeleteIf you are looking to use PHP 5.4 and above, the mysql functions are now deprecated. I would suggest using mysqli or PDO. The latter has more benefits.
ReplyDeletethanks..
ReplyDeletevery useful..
hi,
ReplyDeletehow to open last table page at the beginning?
Its so easy. You know the total number links in $num_page. Yo you can place button with named "last" at first using $num_page. Now when you click the last button, it redirects you to final page.
Deletetqvm its working.. could you show me how to set the first and last page at the pagination?
ReplyDeleteit is very helf full code for pagination
ReplyDeleteall pages is show
ReplyDeleteYou say $maxpage returns number of pages but no $maxpage in code.
ReplyDelete