Pagination is used to display results of your query over multiple pages, rather than displaying on just one page. Pagination prevents user to scroll the webpage. For example if your query is bringing 1000 rows and you want to display these rows in php page then it will be done quite fine but it will disturb the user alot because he/she will tired after scrolling the page Repeatedly.
So to display records properly and to avoid scrolling, we use pagination. So let's look how we can do pagination with php.
*******Pagination.php*********
This is what i want to developed. This pagination offers the facility to Move Next Page, Previous Page, First Page and Last Page.
Pagination.php
<?php
require_once('conn.php');
// how many rows to show per page
$rowsPerPage = 15;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$query = "select * from student" .
" LIMIT $offset, $rowsPerPage";
//print $query;
$result=mysql_query($query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Pagination in php</title>
<style type="text/css">
.fontclass
{font-family:Verdana;
font-size:12px;
}
</style>
</head>
<body>
<table width="100%" cellpadding="2" cellspacing="2" class="fontclass">
<tr>
<td width="12%">
<strong>Roll No</strong></td>
<td width="16%">
<strong>First Name</strong></td>
<td width="20%">
<strong>Last Name</strong></td>
<td colspan="2">
<strong>Father Name</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result))
{
?>
<tr>
<td width="12%">
<?php echo $rows['rollno'];?></td>
<td width="16%">
<?php echo $rows['fname'];?></td>
<td width="20%">
<?php echo $rows['lname'];?></td>
<td width="12%">
<?php echo $rows['fathername'];?></td>
<td width="40%"><strong><a href="delete-rec.php?rollno=<?php echo $rows['rollno'];?>">Delete</a></strong></td>
</tr>
<?php
}
?>
</table>
<?php
$query = "SELECT COUNT(rollno) AS numrows FROM student";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
// creating previous and next link
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo $first . $prev .
" Showing page $pageNum of <B>$maxPage</B> pages " . $next . $last;
?>
</body>
</html>
Explaination
$rowsPerPage = 15;
I am specifying number of rows that will be shown per page .
$pageNum = 1;
By default i will show first page.
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
if page number is coming from query string then i will display that page. Else by default we will show first page.
$offset = ($pageNum - 1) * $rowsPerPage;
Counting the offset, offset is the main thing in whole pagination process. Offset is an integer indicates the distance from beginning until a given element or point.
$query = "select * from student" .
" LIMIT $offset, $rowsPerPage"
Then i apply limit on my query, the above query will look like same if you print it
select * from student LIMIT 0, 15 it means to display number of records that lies in the given range.
After this i am executing the query and displaying rows one by one from result set.
Now again look at some important code snippet in pagination module. After while loop we are counting total number of records by executing this query
$query = "SELECT COUNT(rollno) AS numrows FROM student";
and then i find out the max page, means how many pages will become during whole pagination process and then i find out the first, previous, last and next pages and display them in a proper manner where they need to display.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment