Welcome to The Archive's Forums!
You have stumbled upon the best Gaming, Game Development, and Anime community ever, or at least we think so. We're pretty sure you will too after you see everything we have to offer. Look at our
list of current community projects, browse our
discussion forums, and when you're ready,
sign up! We're glad you're here, and hope you'll join our community!
View Full Version : PHP pageNumberer Class
This is a nice class I created to help with multi-page navigation. I have this version current being implemented on two sites.
http://kniffenart.com/gallery.php // artwork by my father
http://readingdb.com // a search engine that lists books that a certain school has quizzes for. These quizzes are used to test reading comprehnsion.
class pageNumberer{
function pageNumberer($startEntry=0,$totalResults,$perPage, $linkIt,$maxPages=15){
$currentPage=$startEntry/$perPage+1;
$totalPages=ceil($totalResults/$perPage);
if($totalPages>$maxPages){
$totalDisplayPages=$startEntry/$perPage+$maxPages;
$startPage=$startEntry/$perPage+1;
$startPage=$startPage-(ceil($maxPages/2)-1);
$totalDisplayPages=$totalDisplayPages-(ceil($maxPages/2)-1);
if($startPage<1){$startPage=1;
$totalDisplayPages=$maxPages;}
}
else{$totalDisplayPages=$totalPages;$startPage=1;}
if ($totalDisplayPages>$totalPages){
$totalDisplayPages=$totalPages;
$startPage=$totalPages-startPage-$maxPages;
}
$this->startEntry = $startEntry;
$this->currentPage=$currentPage;
$this->totalResults=$totalResults;
$this->totalPages=$totalPages;
$this->totalDisplayPages=$totalDisplayPages;
$this->startPage=$startPage;
$this->perPage=$perPage;
$this->linkIt=$linkIt;
}
function display(){
$linkIt=$this->linkIt.'perPage='.$this->perPage.'&startEntry=';
for($x=$this->startPage;$x<=$this->totalDisplayPages;$x++){
echo' <a';
if($x==$this->currentPage){echo ' class="current ';}
echo ' href="'.$linkIt.($x-1)*$this->perPage.'">'.$x.'</a>';
echo ' ';
}
}
function showingEntries(){
$totalResults=$this->totalResults;
$topRangeDisplayed=$this->startEntry+$this->perPage;
if($topRangeDisplayed>$totalResults){$topRangeDisplayed=$this->totalResults;}
echo 'Showing Results ('.($this->startEntry+1).' - '.$topRangeDisplayed.') out of '.$this->totalResults;
}
function showingPage(){echo'Page '.$this->currentPage.' out of '.$this->totalPages;}
}I plan to extend it soon with a few more options and add a few comments.
I hope some people will be able to find this useful.
neozf
12-25-2006, 05:14 AM
/me 's head hurts
But nice art work.
Neat, that could be useful. Thanks for sharing :)
To alleviate Neo's head hurting and to make the pageNumberer class more useful I have created some documentation for it using my new favorite tool, NaturalDocs (http://naturalDocs.org).
Documentation: http://kniffenwebdesign.com/docs/pageNumberer
Dazed_n_Confused
03-17-2007, 04:42 PM
After several looks, I think I've figured out how it works. I'm guessing that if you were on page 16, this would make it so the lowest page number you would see is 2? It looks like "linkIt" just adds a couple of parameters to the url that each page number links to?
This will be pretty handy for my project, thanks for posting the code.
It would be nice if the current page was maintained as the middle page number though. So, if you were looking at page 16, the highest link shown would be for page 23 and the lowest would be page 9. If it already does that, then my bad.
Woo! That would be great if you did. It would be its first third party implementation.
I also have another php file with several useful functions for using forms with php.
Once I finish documenting it I will post it.
Dazed_n_Confused
03-24-2007, 12:31 AM
I've modified your display function to put <First> and <Last> at the front and end of the links. Also note that I changed linkIt slightly so that I could use the URL I had already begun to generate in my page. 'perPage='.$this->perPage.' was already there for me, so it is still getting used...just not in exactly the same way.
function display(){
$linkIt=$this->linkIt.'&startEntry=';
if (15<=$this->totalPages && $this->startPage!=1) {
echo '<a href="'.$linkIt.'0"><First></a>';
}
for($x=$this->startPage;$x<=$this->totalDisplayPages;$x++){
echo ' <a';
if($x==$this->currentPage){echo ' class="current ';}
echo ' href="'.$linkIt.($x-1)*$this->perPage.'">'.$x.'</a> ';
}
if (15<=$this->totalPages && $this->totalDisplayPages!=$this->totalPages ){
echo ' <a href="'.$linkIt.($this->totalResults-$this->perPage).'"><Last></a>';
}
}
Nice I think I will add that as an option.
Also:
if (15<=$this->totalPages && $this->totalDisplayPages!=$this->totalPages ){
echo ' <a href="'.$linkIt.($this->totalResults-$this->perPage).'"><Last></a>';
}
}
15 should be $maxPages to allow for extendability.
New version now with options! Enjoy!
Options:
'maxPages' => 15
'showFirst' => false
'showLast' => false
'echo' => true
'perPageVar' => 'perPage'
'startEntryVar' => 'startEntry'
Example:
$pageNumbererOptions = array('showFirst' => true,'showLast' => true, 'echo' => false);
$pageNumbers=new pageNumberer($startEntry,$totalResults,$perPage,$l inkIt.'&', $pageNumbererOptions);
Code:
<?
class pageNumberer{
var $startResult;
var $currentPage;
var $totalResults;
var $totalPages;
var $totalDisplayPages;
var $startPage;
var $linkIt;
var $perPage;
var $options = array('maxPages' => 15,
'showFirst' => false,
'showLast' => false,
'echo' => true,
'perPageVar' => 'perPage',
'startEntryVar' => 'startEntry');
function pageNumberer($startResult=0,$totalResults, $perPage, $linkIt, $options = null){
$this->setOptions($options);
$maxPages = $this->options['maxPages'];
$currentPage=$startResult/$perPage+1;
$totalPages=ceil($totalResults/$perPage);
if($totalPages>$maxPages){
$totalDisplayPages=$startResult/$perPage+$maxPages;
$startPage=$startResult/$perPage+1;
$startPage=$startPage-(ceil($maxPages/2)-1);
$totalDisplayPages=$totalDisplayPages-(ceil($maxPages/2)-1);
if($startPage<1){$startPage=1;
$totalDisplayPages=$maxPages;}
}
else{$totalDisplayPages=$totalPages;$startPage=1;}
if ($totalDisplayPages>$totalPages){
$totalDisplayPages=$totalPages;
$startPage=$totalPages-startPage-$maxPages;
}
$this->endPage = $startPage + $totalDisplayPages;
$this->startEntry = $startResult;
$this->currentPage = $currentPage;
$this->totalResults = $totalResults;
$this->totalPages = $totalPages;
$this->totalDisplayPages = $totalDisplayPages;
$this->startPage = $startPage;
$this->perPage = $perPage;
$this->linkIt = $linkIt.'&'.$this->options['perPageVar'].'='.$this->perPage.
'&'.$this->options['startEntryVar'].'=';
$this->currentPageLink = $this->linkIt.$this->startEntry;
}
function display(){
if($this->options['showFirst'] == true && $this->startPage > 1){
$display = '<a href="'.$this->linkIt.'0">First</a>';
}
for($x=$this->startPage;$x<=$this->totalDisplayPages;$x++){
$display .= ' <a';
if($x==$this->currentPage) $display .= ' class="current ';
$display .= ' href="'.$this->linkIt.($x-1)*$this->perPage.'">'.$x.'</a> ';
}
if($this->options['showLast'] == true && $this->endPage < $this->totalPages-1){
$display .= '<a href="'.$this->linkIt.(($this->totalPages-1)*$this->perPage).'">Last</a>';
}
if($this->options['echo'] == true) echo $display;
else return $display;
}
function showingEntries(){
$totalResults=$this->totalResults;
$topRangeDisplayed=$this->startEntry+$this->perPage;
if($topRangeDisplayed>$totalResults){$topRangeDisplayed=$this->totalResults;}
$display = 'Showing Results ('.($this->startEntry+1).' - '.$topRangeDisplayed.') out of '.$this->totalResults;
if($this->options['echo'] == true) echo $display;
else return $display;
}
function showingPage(){
$display = 'Page '.$this->currentPage.' out of '.$this->totalPages;
if($this->options['echo'] == true) echo $display;
else return $display;
}
function setOptions($options){
foreach($options as $key => $value){
if($this->isValidOption($key)) $this->options[$key] = $value;
}
}
function isValidOption($name){ return array_key_exists($name, $this->options); }
function getCurrentPage(){ return $this->currentPageLink; }
}
?>
vBulletin® v3.8.5, Copyright ©2000-2010, Jelsoft Enterprises Ltd.