<?Php
/**
* This class performs paging to use mysql queries
* PHP version 4 or 5
*
* @category Mysql Pager Class
* @author Zafer Karaca <hide@address.com>
* @version v 1.0 2011/03/01
* @link http://www.creathink.net
*/
class Pager {
// variable keep the page number
public $PageNumber=1;
// Calculate how many records are displayed on one page
var $PerPages = 10;
// Calculates the number of links per page would
var $PageNumberLimit=10;
// Gives the starting number for the records in mysql
var $PerPageStart=0;
//mysql connecttion
var $dbc;
function __construct($db,$itemPerPage) {
$this->dbc = $db;
$this->PerPages = $itemPerPage;
}
function sql_query($sql) {
// Sql query for total records
//$this->sql_num = mysql_query($sql);
$this->sql_num = $this->dbc->query($sql);
// Sql query for limit records
$this->PageCalc();
//$this->sql=mysql_query("$sql LIMIT $this->PerPageStart , $this->PerPages");
$this->sql=$this->dbc->query("$sql LIMIT $this->PerPageStart , $this->PerPages");
}
function TotalPage() {
// Total number of pages to find this
// $this->PerPages Each page will record how many, and divide the Total Number of Records with the number of Total Page Appears
// ceil functionu rounding the total number of pages.
$this->TotalPage = $this->TotalRecord() / $this->PerPages;
return ceil($this->TotalPage);
}
function TotalRecord() {
// We find the total number of records using mysql_num_rows.
//$this->TotalRecord = @mysql_num_rows($this->sql_num);
$this->TotalRecord = @$this->sql_num->size();
return $this->TotalRecord;
}
function RemainingPage() {
// Remaining Page calc.
// ceil functionu ile kalan sayfa sayısını yuvarlıyoruz.
$this->RemainingPage = $this->TotalPage() - $this->PageNumber;
return $this->RemainingPage;
}
function PageCalc() {
/// if you are taking a page number, page number is greater than the total pages
if($this->PageNumber>$this->TotalPage() or empty($this->PageNumber)) {
$this->PageNumber=1;
}
// we find the starting number for mysql.
$this->PerPageStart = (($this->PageNumber-1) * $this->PerPages+1)-1;
}
// Pages are preparing for the links.
function PageLink() {
$this->calc = ceil(($this->PerPageStart) / $this->PerPages / $this->PageNumberLimit);
for($b=0;$b <= $this->calc ;$b++){
$this->StartCalc = $b * $this->PageNumberLimit;
$this->EndCalc = $this->StartCalc + $this->PageNumberLimit ;
if ($this->EndCalc <= $this->TotalPage) {
if($this->PageNumber >= $this->StartCalc) {
$this->End = $this->EndCalc;
$this->Start =$this->StartCalc;
}
}
if ($this->EndCalc >= $this->TotalPage) {
if($this->PageNumber >= $this->StartCalc) {
$this->End= $this->TotalPage;
$this->Start=$this->StartCalc;
}
}
if(empty($this->Start)) {
$this->Start=1;
}
}
for($i=ceil($this->Start); $i <= ceil($this->End) ;$i++) {
@$list[$i]=$this->i;
}
return $list;
}
function mysql_fetch() {
// We take the data from the database using mysql_fetch_array.
//while($row= mysql_fetch_array($this->sql)) {
while($row= $this->sql->fetch()) {
$rows[] = $row;
}
return $rows;
}
// find the previous page number
function PreviousPage() {
if($this->PageNumber) {
$PreviousNumber = $this->PageNumber-1;
}
return $PreviousNumber;
}
// find the next page number
function NextPage() {
if ($this->PageNumber < $this->TotalPage) {
$NextNumber = $this->PageNumber+1;
}
return @$NextNumber;
}
// We find the current page Number
function PageNumber() {
return $this->PageNumber;
}
} // end class
?>