PHP Pagination Creation with OOP

Hi!!

At First you need to create a file called “Pagination.php”
then paste code in this file bilow:

class Pagination extends db {

public $perpage = 0;
public $numberOfPages;
public $numRows;
public $totalRows;
public $tableName;
public $active;

public function __construct($pageNumber,$tableName){

$this->tableName = $tableName;

$this->perpage = $pageNumber;

$this->totalRows = $this->query("select * from `$tableName`");

$this->num_row = $this->num_rows($this->totalRows);

$this->numberOfPages = ceil($this->num_row/$this->perpage);

}

public function viewList($pageNo){
$limitStart = $this->perpage * ($pageNo-1);
$this->query = $this->query("select * from `$this->tableName` limit $limitStart,$this->perpage");
$this->active = $pageNo;
return $this->query;
}

public function pagination($pageName,$view,$pg){

$a = "

    if($pg != '1')
    $b = $pg-1 ;
    else
    $b = '1';

$c = "'>

";
$d = "";

for($i = 1; $i <= $this->numberOfPages; $i++)
if($this->active == $i)
$d .= "

    $i

";
else
$d .= "

    $i

";

$e ="

Pages 1 of $this->numberOfPages

";

return $pagination = $a.$b.$c.$d.$e.$f.$g;
}
}

 

 

Now in which page you want to use pagination you just need to include a this page and make an object.
eg.

include_once('Pagination.php');
$pag = new Pagination(10,"shipment");

// here $pag is an object of class called Pagination
// 10 is row per page,
// "shipment" is the table name. from which table you want to show list.

Now use $pag->viewList($_GET['pg']);
to show the paginated row list.
// $_GET['pg'] no is the sagmented individual page no.

Then use $pag->pagination("orders","view",$_GET['pg']);
// "order" is name of the page that you want to use pagination
// "view" is the get method variable eg. $_GET['view']
// $pg is current page no
// i mean to say the path was "http://domain.com/orders?view=list&pg=1"
....

 

 

 

Advertisements

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