Tweet

Sunday, 25 March 2012

Pagination Example


<?php
$connection = mysql_connect("localhost","root","");
$database = mysql_select_db('database');






$numOfRecordPerpage = 5;
$numofPage = 1;

if(isset($_REQUEST['page'])){
$numofPage = $_REQUEST['page'];
}

$start = ($numofPage-1)* $numOfRecordPerpage;

$query = mysql_query("select * from tablename limit $start,$numOfRecordPerpage");



echo "<table border='0' width='60%' bgcolor='lightgreen' align='center'>";
echo "<tr>";echo "<td>";echo "Name";echo "</td>";echo "<td>";echo "Age";echo "</td>";echo "<td>";echo "City";
echo "</td>";echo "<td>";echo "Salary";echo "</td>";echo "</tr>";
echo "<tr>";


while($result = mysql_fetch_array($query)){


echo "<td bgcolor='#ffffcc'>".$result['user_name']."</td>";
echo "<td bgcolor='#ffffcc'>".$result['user_age']."</td>";
echo "<td bgcolor='#ffffcc'>".$result['user_city']."</td>";
echo "<td bgcolor='#ffffcc'>".$result['user_salary']."</td>";
echo "</tr>";
}
echo "</table>";



$query = mysql_query("select * from tablename");
$totalNumofRecord = mysql_num_rows($query);


$maxPageNo = ceil($totalNumofRecord/$numOfRecordPerpage);
 
$self = $_SERVER['PHP_SELF'];


if($numofPage>1)

{
$currentPage = $numofPage-1;
$prvious = "<a href=\"$self?page=$currentPage\"><b>prvious</b></a>";
$first = "<a href=\"$self?page=1\"><b>First</b></a>";
}

else

{
$prvious = "Previous";
$first = "First";
}


if($numofPage<$maxPageNo)

{
$currentPage = $numofPage+1;
$next = "<a href=\"$self?page=$currentPage\"><b>next</b></a>";
$last = "<a href=\"$self?page=$maxPageNo\"><b>last</b></a>";
}

else

{
$next = "next";
$last = "last";
}
//echo $first." ".$prvious." ".$next." ".$last;


//echo $first."".$prev." ";
for($i=1;$i<=$maxPageNo;$i++)

{
     
 if($numOfPage == $i)

{
echo " "."<a href=\"$self?page=$i\"><b>$i</b></a>"." ";
}

   else
   {
echo " "."<a href=\"$self?page=$i\">$i</a>"." ";
   }
  }
 // echo " ".$next." ".$last;

?>

No comments:

Post a Comment