Handy pagination PHP class with MySql


What if you have a table with a thousand rows, and you want to allow the user to browse through the entire table. Simply listing all the records in that table would not be a good idea. Instead you should break the table up into smaller “chunks” and allow the user to navigate through theses “chucks”. This is what pagination does, it allows you to break up large result sets from a database query, and present it to the user in a more manageable way.

As your database grows, showing all the results of a query on a single page is no longer practical. This is where pagination comes in handy. You can display your results over a number of pages, each linked to the next, to allow your users to browse your content in bite sized pieces.

This css put your css file or your file head section

<style type=”text/css”>
a.linksitem:link, a.linksitem:visited{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
text-decoration:none;
font-weight:normal;
width:auto;
height:20px;
border:1px solid #75ABEA;
background-color:#FFFFFF;
color:#2E6AB1;
padding-right:5px;
padding-left:5px;
}
a.linksitem:hover{
border:1px solid #2E6AB1;
color:#000000;
}
.selectedlinks{
border:1px solid #2E6AB1;
background-color:#2E6AB1;
color:#FFFFFF;
font-weight:800;
padding-right:5px;
padding-left:5px;
}
</style>

This is the PHP class for pagination

<?php
class pagination{

var $p=1, $max_r, $limits;
var $count_all=0, $sql, $total, $table, $totalres, $totalpages;
var $r, $i;
var $show=10;

function connect($host,$username,$password,$name){
//—–connect mysql—–//
$connectect = mysql_connect($host, $username, $password) or die(mysql_error());
$selected = mysql_select_db($name) 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($titlebar, $rowsitem){
echo “<b>Total Values(s):</b>”.$this->count_all.”<br><br>”;
echo “<b>Page:</b> “.$this->p.”<br>”;
$fields=mysql_num_fields($this->totalres);
echo ‘<table border=”1″ width=”100%” cellspacing=”0″ cellpadding=”4″><tr>’;
for ($i=0; $i < mysql_num_fields($this->sql); $i++) //Table Header
{
print “<th class=’titlebar’>”.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 class=’rowsitem’>$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 class=’linksitem’ href=?p=1> First </a>&nbsp;”;
}
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 class=’linksitem’ href=?p=$previous> Previous </a>&nbsp;”;
}
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 “<span class=’selectedlinks’>”.$this->p.”</span>”;
}
else{
echo ” <a class=’linksitem’ href=?p=”.$this->p.”>”.$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 class=’linksitem’ href=?p=”.$this->totalpages.”>”.$this->totalpages.”</a>”;
}
if($_GET[“p”] < $this->totalpages)// if p is less then total pages then show next link
{
$next = $_GET[“p”] + 1;
echo “&nbsp;<a class=’linksitem’ href=?p=$next> Next </a>”;
}
echo “<br><br>”;
}
}
?>

Now call pagination class there you may use pagination

<?php
$page= new pagination; //Create object
$page->connect(“servername”,”username”,”password”,”batabasename”); //Create database connection
//$page->connect(“localhost”,”root”,””,”test”); // example
$page->setMax(10); //Number of items display
$page->setData(“tablename”); //Tables name here
$page->display(rana, test); //Display data
$page->displayLinks(9); //Display links
?>

Download code

Advertisements

12 thoughts on “Handy pagination PHP class with MySql

  1. Thanks for posting the code. It seems very nice. But, shouldn’t the connection be separate from the pagination and the pagination process from the view? That way, we could have a pagination in CSS or AS3 for example. 🙂

    I’m just suggesting another article, because I’m absolutly clueless about how to archive that. :But I believe that a pagination class should contain only the pagination logic.

    Thanks once again.

  2. I have used this code. Thank u very much.Sir I am quite confuse about cake php .Would u mind tell it about?with full of code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s