paging with class and ajax
imran.php
<?php
class pagination{
var $row;
var $p=1;
var $limits;
var $count_all=0,$sql,$total,$table,$totalres,$totalpages;
var $r,$i;
function connect(){
$connectect = mysql_connect("localhost","root","") or die(mysql_error());
$selected = mysql_select_db(mydata) or die(mysql_error());
}
function setMax($max_r){
$this->p = $_GET['p'];
$this->max_r = $max_r;
if(empty($this->p))
{
$this->p = 1;
}
$this->limits = ($this->p - 1) * $this->max_r;
}
function setData($table){
$this->table = $table;
$this->sql = "SELECT * FROM ".$this->table." LIMIT ".$this->limits.",".$this->max_r."";
$this->sql = mysql_query($this->sql) or die(mysql_error());
$this->total = "SELECT * FROM ".$this->table."";
$this->totalres = mysql_query($this->total) or die(mysql_error());
$this->count_all = mysql_num_rows($this->totalres);
$this->totalpages = ceil($this->count_all / $this->max_r);
}
function display(){
$fields=mysql_num_fields($this->totalres);
echo "<table border=1 width=100%><tr>";
for ($i=0; $i < mysql_num_fields($this->sql); $i++) //Table Header
{
print "<th>".mysql_field_name($this->sql, $i)."</th>";
}
echo "</tr>";
while ($row = mysql_fetch_row($this->sql))
{
echo "<tr>";
for ($f=0; $f < $fields; $f++)
{
echo "<td>$row[$f]</td>";
}
echo "</tr>\n";
}
echo "</table><p>";
}
function displayLinks($show){
$this->show = $show; // How many links to show
echo "<br><br>";
if($this->p > 1) // If p > then one then give link to first page
{
echo "<a href=?p=1> [FIRST] </a> ";
}
else{ // else show nothing
echo "";
}
if($this->p != 1){ // if p aint equal to 1 then show previous text
$previous = $this->p-1;
echo "<a href=?p=$previous> [ PREVIOUS ] </a>";
}
else{ //else show nothing
echo "";
}
for($i =1; $i <= $this->show; $i++) // show ($show) links
{
if($this->p > $this->totalpages){ // if p is greater then totalpages then display nothing
echo "";
}
else if($_GET["p"] == $this->p){ //if p is equal to the current loop value then dont display that value as link
echo $this->p;
}
else{
echo " <a href=?p=".$this->p."&maxrow=".$this->max_r."> ( ".$this->p.") </a>"; // else display the rest as links
}
$this->p++; //increment $p
}
echo "....."; // display dots
if($_GET["p"] == $this->totalpages){// if page is equal to totalpages then dont display the last page at the end of links
echo "";
}
else // else display the last page link after other ones
{
echo "<a href=?p=".$this->totalpages."&maxrow=".$this->max_r."> ( ".$this->totalpages.") </a>";
}
if($_GET["p"] < $this->totalpages)// if p is less then total pages then show next link
{
$next = $_GET["p"] + 1;
echo "<a href=?p=$next&maxrow=".$this->max_r."> [ NEXT >] </a>";
}
echo "<br><br>";
}
}
?>
<html>
<head>
<title> paging with class and ajax</title>
<script type="text/javascript">
function showUser(str)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("paging").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","paging.php?maxrow="+str,true);
xmlhttp.send(); }
</script>
</head>
<body>
<form name="frm" method="post" >
select row per page
<select name="users" onChange="showUser(this.value)">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</form>
<br />
<div id="paging">
<?php
$page= new pagination;
$page->connect();
if(isset($_REQUEST['maxrow'])){
$page->setMax($_REQUEST['maxrow']);
}else{
$page->setMax(2);
}
$page->setData("login");
$page->display();
$page->displayLinks(5);
?>
</div>
</body>
</html>
paging.php
<?php
$row=$_GET['maxrow'];
class pagination{
var $row;
var $p=1;
var $limits;
var $count_all=0,$sql,$total,$table,$totalres,$totalpages;
var $r,$i;
function connect(){
$connectect = mysql_connect("localhost","root","") or die(mysql_error());
$selected = mysql_select_db(mydata) or die(mysql_error());
}
function setMax($max_r){
$this->p = $_GET['p'];
$this->max_r = $max_r;
if(empty($this->p))
{
$this->p = 1;
}
$this->limits = ($this->p - 1) * $this->max_r;
}
function setData($table){
$this->table = $table;
$this->sql = "SELECT * FROM ".$this->table." LIMIT ".$this->limits.",".$this->max_r."";
$this->sql = mysql_query($this->sql) or die(mysql_error());
$this->total = "SELECT * FROM ".$this->table."";
$this->totalres = mysql_query($this->total) or die(mysql_error());
$this->count_all = mysql_num_rows($this->totalres);
$this->totalpages = ceil($this->count_all / $this->max_r);
}
function display(){
$fields=mysql_num_fields($this->totalres);
echo "<table border=1 width=100%><tr>";
for ($i=0; $i < mysql_num_fields($this->sql); $i++) //Table Header
{
print "<th>".mysql_field_name($this->sql, $i)."</th>";
}
echo "</tr>";
while ($row = mysql_fetch_row($this->sql))
{
echo "<tr>";
for ($f=0; $f < $fields; $f++)
{
echo "<td>$row[$f]</td>";
}
echo "</tr>\n";
}
echo "</table><p>";
}
function displayLinks($show){
$this->show = $show; // How many links to show
echo "<br><br>";
if($this->p > 1) // If p > then one then give link to first page
{
echo "<a href=?p=1&maxrow=".$this->max_r."> [FIRST] </a> ";
}
else{ // else show nothing
echo "";
}
if($this->p != 1){ // if p aint equal to 1 then show previous text
$previous = $this->p-1;
echo "<a href=?p=$previous&maxrow=".$this->max_r."> [ PREVIOUS ] </a>";
}
else{ //else show nothing
echo "";
}
for($i =1; $i <= $this->show; $i++) // show ($show) links
{
if($this->p > $this->totalpages){ // if p is greater then totalpages then display nothing
echo "";
}
else if($_GET["p"] == $this->p){ //if p is equal to the current loop value then dont display that value as link
echo $this->p."&maxrow=".$this->max_r;
}
else{
echo " <a href=?p=".$this->p."&maxrow=".$this->max_r."> ( ".$this->p.") </a>"; // else display the rest as links
}
$this->p++; //increment $p
}
echo "....."; // display dots
if($_GET["p"] == $this->totalpages){// if page is equal to totalpages then dont display the last page at the end of links
echo "";
}
else // else display the last page link after other ones
{
echo "<a href=?p=".$this->totalpages."&maxrow=".$this->max_r."> ( ".$this->totalpages.") </a>";
}
if($_GET["p"] < $this->totalpages)// if p is less then total pages then show next link
{
$next = $_GET["p"] + 1;
echo "<a href=?p=$next&maxrow=".$this->max_r."> [ NEXT >] </a>";
}
echo "<br><br>";
}
}
$page= new pagination;
$page->connect();
$page->setMax($row);
$page->setData("login");
$page->display();
$page->displayLinks(5);
?>