Hello.
I'm releasing my pagination class.
I already wrote it in another topic while helping someone, so I thought I'd just make an own topic for it.
I've improoved it since I posted it in that other topic, and now I've written descriptions of each function.
Also, this *should* work without MySQL, eventhough my example is based on MySQL.
php Code:
<?php/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2008
||||||||||||||||||||||||||||||||||||||||||
**/ class pagination
{ // The total values. private $totalPages;
private $totalResults;
private $totalPerPage;
// The current values. private $currentPage;
// The first result on current page. private $firstResult;
/**
* Sets the maximum allowed results per page
*
* @param integer $max, default 10
**/ public function setMax
($max =
10) { if(is_numeric($max)) { $this->
totalPerPage =
$max;
} } /**
* Generate the first result on the current page
*
* @param integer $page, which page we're currently viewing
* @return integer first result
**/ public function setPage
($page) { if(is_numeric($page)) { $this->
currentPage =
mysql_real_escape_string($page);
$this->
firstResult =
(($this->
currentPage *
$this->
totalPerPage) -
$this->
totalPerPage);
return $this->
firstResult;
} } /**
* Generates how many pages based on the total amount of results
*
* @param array $results, an array of all the results
* @return array of pages
**/ public function getPages
($results) { $this->
totalResults =
count($results);
$totalPages =
$this->
totalResults /
$this->
totalPerPage;
$this->
totalPages =
ceil($totalPages);
$x =
1;
$array =
array();
while($x <=
$this->
totalPages) { $array[] =
$x;
$x++;
} return $array;
} /**
* Checks if a link is valid
*
* @param integer $pagenr, the number of the page you want to check if it exist
* @return true or false
**/ public function checkLink
($pagenr) { if($pagenr <=
$this->
totalPages &&
$pagenr >=
1) { return true;
} return false;
} /**
* Generate the current page number
*
* @return array, [0] = the current page, [1] = total pages
**/ public function getCurrentPage
() { $array =
array();
$array[] =
$this->
currentPage;
$array[] =
$this->
totalPages;
return $array;
} }?>
Example usage:
php Code:
<?php/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2008
||||||||||||||||||||||||||||||||||||||||||
**/ include('pagination.php');
$p =
$_GET[
'p'];
(isset($p)) ?
$p :
1;
$max =
10;
// Create the object and set some basic values. $pagination =
new pagination
();
$pagination->
setMax($max);
// Get the total results, and then calculate how many pages that becomes based on how many results per page. $totSql =
"SELECT * FROM `table`";
$totQuery =
mysql_query($totSql) or
die(mysql_error());
$totResults =
mysql_fetch_array($totQuery);
$totPages =
$pagination->
getPages($totResults);
// Set the current page, and get the first result on it. $first =
$pagination->
setPage($p);
// Get the results of the current page. $exSql =
"SELECT * FROM `table` LIMIT $first, $max";
$exQuery =
mysql_query($exSql) or
die(mysql_error());
$exResults =
mysql_fetch_array($exQuery);
// Echo out the current page, and the total amount of pages. $page =
$pagination->
getCurrentPage();
echo 'Page: '.
$page[
0].
' of '.
$page[
1];
// Get previous page link, and check if it's valid. $prevLink =
$p -
1;
if($pagination->
checkLink($prevLink)) { echo '<a href="example.php?page='.
$prevLink.
'">Previous Page</a>';
} // Print all the pages foreach($totPages as $pageNumber) { if($pageNumber ==
$p) { // The markup for showing that this is the current page is currently <strong> echo '<a href="example.php?page='.
$pageNumber.
'"><strong>'.
$pageNumber.
'</strong></a>';
} else { echo '<a href="example.php?page='.
$pageNumber.
'">'.
$pageNumber.
'</a>';
} } // Get the next page link, and check if it's valid. $nextLink =
$p +
1;
if($pagination->
checkLink($nextLink)) { echo '<a href="example.php?page='.
$nextLink.
'">Next Page</a>';
} foreach($exResults as $news) { echo $news[
'news_title'];
echo '<br /><br />';
} ?>
I started working on an example to use this with a gallery, where the image files were in a image dir. But I got stuck.
More specificly I got stuck when I was trying to get the results within the first result on a page, and the last result on a page, based on the value of the max nr of results per page.
Maybe someone else can try to work it out?
Anyways, this is my pagination class.
Any comments?
