How to create pagination? Paginate in PHP

Okay so how many of you have been stuck in situations where you have plenty of records to display from a table and you cannot divide the records into pages. And your page becomes as long as “Great Wall of China”.  This has been situation for most of the people at least once. So today I am going to show you how to create pagination in PHP.

Well here is the code that you can use to create pagination. I have explained the code with the help of comments for each line, so won’t be explaining too much here.

But remember to replace the fields in the connection parameter with your values.

<?php

// code for connecting to database. Replace with your connection parameters.

$conn=mysql_connect(‘localhost’,'username’,'password’) or die(“Could Not Connect to server”);

$db=mysql_select_db(“database name”,$conn) or die(“Database not found.”);

$sql=”select * from tablename”;

$result1=mysql_query($sql);

$rows = mysql_num_rows($result1);

// Get the current Pagenumber

if(isset($_GET['pagenum']))

{

$pagenum = $_GET['pagenum'];

}

else

{

$pagenum = 1; // if pagenumber is not set, then set it to 1.

}

//This is the number of results displayed per page

$page_rows = 4;

//This tells us the page number of our last page

$last = ceil($rows/$page_rows);

//this makes sure the page number isn’t below one, or more than our maximum pages

if ($pagenum < 1)

{

$pagenum = 1;

}

elseif ($pagenum > $last)

{

$pagenum = $last;

}

// set the limit for search as how many search from which record. and query again.

$offset = ($pagenum – 1) * $page_rows;

$data_p = mysql_query(“SELECT * FROM tablename”.”  LIMIT $offset, $page_rows”) or die(“No data found!!”);

// Show Records.

while($res=mysql_fetch_array($data_p))

{

echo $res["columnname"].”</br>”;

}

// showing pagination links.

echo “<div><ul>”;

echo “<font face=’verdana’ size = ‘2′ > –You are currently at page $pagenum of $last– </font><p>”;

// First we check if we are on page one. If we are then we don’t need a link to the previous page or the first page so we do nothing. If we aren’t then we generate links to the first page, and to the previous page.

if ($pagenum == 1)

{

}

else

{

echo “<li> <a href=’{$_SERVER['PHP_SELF']}? pagenum=1′><font face=’verdana’ size = ‘2′ >  First </font></a> </li>”;

echo ” “;

$previous = $pagenum-1;

echo “<li> <a href=’{$_SERVER['PHP_SELF']}?pagenum=$previous’><font face=’verdana’ size = ‘2′ >  Previous </font></a> </li>”;

echo ” “;

}

//just a spacer

echo ”  ”;

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links

if ($pagenum == $last)

{

}

else {

$next = $pagenum+1;

echo “<li> <a href=’{$_SERVER['PHP_SELF']}? pagenum=$next’><font face=’verdana’ size = ‘2′ >  Next </font></a></li> “;

echo ” “;

echo “<li> <a href=’{$_SERVER['PHP_SELF']}? pagenum=$last’><font face=’verdana’ size = ‘2′ >  Last </font></a></li> “;

}

echo “</ul></div>”;

?>

Pagination in PHP
Okay so how many of you have been stuck in situations where you have plenty of records to display from a table and you cannot divide the records into pages. And your page becomes as long as “Great Wall of China”.  This has been situation for most of the people at least once. So today I am going to show you how to create pagination in PHP.
Well here is the code that you can use to create pagination. I have explained the code with the help of comments for each line, so won’t be explaining here.
But remember to replace the fields in the connection parameter with your values.
<?php
// code for connecting to database. Replace with your connection parameters.
$conn=mysql_connect(‘localhost’,'username’,'password’) or die(“Could Not Connect to server”);
$db=mysql_select_db(“database name”,$conn) or die(“Database not found.”);
$sql=”select * from tablename”;
$result1=mysql_query($sql);
$rows = mysql_num_rows($result1);
// Get the current Pagenumber
if(isset($_GET['pagenum']))
{
$pagenum = $_GET['pagenum'];
}
else
{
$pagenum = 1; // if pagenumber is not set, then set it to 1.
}
//This is the number of results displayed per page
$page_rows = 4;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn’t below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
// set the limit for search as how many search from which record. and query again.
$offset = ($pagenum – 1) * $page_rows;
$data_p = mysql_query(“SELECT * FROM tablename”.”  LIMIT $offset, $page_rows”) or die(“No data found!!”);
// Show Records.
while($res=mysql_fetch_array($data_p))
{
echo $res["columnname"].”</br>”;
}
// showing pagination links.
echo “<div><ul>”;
echo “<font face=’verdana’ size = ‘2′ > –You are currently at page $pagenum of $last– </font><p>”;
// First we check if we are on page one. If we are then we don’t need a link to the previous page or the first page so we do nothing. If we aren’t then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo “<li> <a href=’{$_SERVER['PHP_SELF']}? pagenum=1′><font face=’verdana’ size = ‘2′ >  First </font></a> </li>”;
echo ” “;
$previous = $pagenum-1;
echo “<li> <a href=’{$_SERVER['PHP_SELF']}?pagenum=$previous’><font face=’verdana’ size = ‘2′ >  Previous </font></a> </li>”;
echo ” “;
}
//just a spacer
echo ”  ”;
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo “<li> <a href=’{$_SERVER['PHP_SELF']}? pagenum=$next’><font face=’verdana’ size = ‘2′ >  Next </font></a></li> “;
echo ” “;
echo “<li> <a href=’{$_SERVER['PHP_SELF']}? pagenum=$last’><font face=’verdana’ size = ‘2′ >  Last </font></a></li> “;
}
echo “</ul></div>”;
?>

Post a Comment