| 0 comments ]

One of my Friend Created a class For Pagination in PHP . This class can be used to split MySQL database query listings between multiple pages.

It takes as parameters a SQL query, the limit number of result rows to display per page, and the number of the listing page being displayed.

The class executes the query to retrieve the results of the current page and stores the result handle in a class variable.

The class also generates HTML with links to go to the first, last, next, previous and any intermediate page of the listing.

It also generates HTML to display the current page being display and the total number of rows in the result set.

The links trigger the call of a Javascript function that will switch to the page associated with the link.

The PHP Class:

<?
/*
Developed by Reneesh T.K
reneeshtk@gmail.com
You can use it with out any worries...It is free for you..It will display the out put like:
First | Previous | 3 | 4 | 5 | 6 | 7| 8 | 9 | 10 | Next | Last
Page : 7 Of 10 . Total Records Found: 20
*/
class Pagination_class{
var $result;
var $anchors;
var $total;
function Pagination_class($qry,$starting,$recpage)
{
$rst = mysql_query($qry) or die(mysql_error());
$numrows = mysql_num_rows($rst);
$qry .= " limit $starting, $recpage";
$this->result = mysql_query($qry) or die(mysql_error());
$next = $starting+$recpage;
$var = ((intval($numrows/$recpage))-1)*$recpage;
$page_showing = intval($starting/$recpage)+1;
$total_page = ceil($numrows/$recpage);

if($numrows % $recpage != 0){
$last = ((intval($numrows/$recpage)))*$recpage;
}else{
$last = ((intval($numrows/$recpage))-1)*$recpage;
}
$previous = $starting-$recpage;
if($previous < 0){
$anc = "First | Previous | ";
}else{
$anc .= "<a href='javascript:pagination(0);'>First | </a>";
$anc .= "<a href='javascript:pagination($previous);'>Previous | </a>";
}

################If you dont want the numbers just comment this block###############
$norepeat = 4;//no of pages showing in the left and right side of the current page in the anchors
$j = 1;
for($i=$page_showing; $i>1; $i--){
$fpreviousPage = $i-1;
$page = ceil($fpreviousPage*$recpage)-$recpage;
$anch = "<a href='javascript:pagination($page);'>$fpreviousPage | </a>".$anch;
if($j == $norepeat) break;
$j++;
}
$anc .= $anch;
$anc .= $page_showing ."| ";
$j = 1;
for($i=$page_showing; $i<$total_page; $i++){
$fnextPage = $i+1;
$page = ceil($fnextPage*$recpage)-$recpage;
$anc .= "<a href='javascript:pagination($page);'>$fnextPage | </a>";
if($j==$norepeat) break;
$j++;
}
############################################################
if($next >= $numrows){
$anc .= "Next | Last ";
}else{
$anc .= "<a href='javascript:pagination($next);'>Next | </a>";
$anc .= "<a href='javascript:pagination($last);'>Last</a>";
}
$this->anchors = $anc;

$this->total = "<svaluestrong>Page : $page_showing <i> Of </i> $total_page . Total Records Found: $numrows</svaluestrong>";
}
}
?>


Demo Page:


<?php

include('pagination_class.php');
mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
mysql_select_db('std_db');


/*
-- Table structure for table `students`

CREATE TABLE `students` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`)
);

INSERT INTO `students` VALUES (1, 'Reneesh');
INSERT INTO `students` VALUES (2, 'Aniesh');
INSERT INTO `students` VALUES (3, 'Babu');
INSERT INTO `students` VALUES (4, 'Antony');
INSERT INTO `students` VALUES (5, 'Praveesh');
INSERT INTO `students` VALUES (6, 'Dixon');
INSERT INTO `students` VALUES (7, 'Sanju');
INSERT INTO `students` VALUES (8, 'Neeraj');
INSERT INTO `students` VALUES (9, 'Siju');
INSERT INTO `students` VALUES (10, 'Noble');
INSERT INTO `students` VALUES (11, 'Bibin');
INSERT INTO `students` VALUES (12, 'Febin');
INSERT INTO `students` VALUES (13, 'Binu');
INSERT INTO `students` VALUES (14, 'Charles');
INSERT INTO `students` VALUES (15, 'jaggu');
INSERT INTO `students` VALUES (16, 'mani');
INSERT INTO `students` VALUES (17, 'milu');
INSERT INTO `students` VALUES (18, 'aravind');
INSERT INTO `students` VALUES (19, 'jay');
INSERT INTO `students` VALUES (20, 'hari');
*/
?>

<script language="JavaScript">
function pagination(page)
{
window.location = "testpage.php?search_text="+document.form1.search_text.value+"&starting="+page;
}
</script>
<?
$qry = "SELECT * FROM students";

if($_REQUEST['search_text']!=""){
$searchText = $_REQUEST['search_text'];
$qry .=" where name like '$searchText%'";
}

//for pagination
if(isset($_GET['starting'])&& !isset($_REQUEST['submit'])){
$starting=$_GET['starting'];
}else{
$starting=0;
}
$recpage = 2;//number of records per page

$obj = new pagination_class($qry,$starting,$recpage);
$result = $obj->result;


?><form name="form1" action="testpage.php" method="POST">

<table border="1" width="40%">
<tr>
<TD colspan="2">
Search <input type="text" name="search_text" value="<? echo $searchText;?>">
<input type="submit" value="Search">
</TD>
</tr>
<tr><TD>Sl no</TD><TD>Name</TD></tr>
<?if(mysql_num_rows($result)!=0){
$counter = $starting + 1;
while($data = mysql_fetch_array($result)) {?>
<tr>
<TD><? echo $counter; ?></TD>
<TD><? echo $data['name']; ?></TD>
</tr><?
$counter ++;
} ?>


<tr><TD align="center" colspan="2"><? echo $obj->anchors; ?></TD></tr>
<tr><TD align="center" colspan="2"><? echo $obj->total; ?></TD></tr>
<?}else{
echo "No Data Found";
}?>
</TD></tr></table></form>

Read Me:

Developed by Reneesh T.K
reneeshtk@gmail.com

You can use it with out any worries...It is free for you..It will display the out put like:

First | Previous | 3 | 4 | 5 | 6 | 7| 8 | 9 | 10 | Next | Last
Page : 7 Of 10 . Total Records Found: 20

Just pass the query ,records per page and starting from the page you want to display the pagination.

Advantage: It is connected to search process in the example and you may extend this feature as you like..It can also use in ajax by putting the contents of the javascript function in the ajaxfunction used for ajax.

Or You can Download it from PhpClasses.org

For Zip http://www.phpclasses.org/browse/package/4318/download/zip.html

or

For Tar http://www.phpclasses.org/browse/package/4318/download/targz.html

0 comments

Post a Comment

Please put your comments here. your questions, your suggestions, also what went wrong with me.