PHP_Class_Code::pageList
Date: 12/24/10
(Web Development) Keywords: php, java
One of a few things I've put together to help on some projects I've been working on. Hopefully you can make use of this, or tear it to pieces if you think it demonstrates poor coding practices. I've tried to keep the documentation as short and concise as possible.
This started out as a jQuery plugin, but I prefer to use the PHP one in normal operation rather than populating post-DOM load. The jQuery one has been relegated to creating pages for AJAX calls only.
My normal output is << First < Previous 7 8 [9] 10 11 Next > Last >>
/* PHP Paginator
Usage : $var=new pageList(array('link'=>'http://linktouse.com/index.php?page=#' [,opt page=x [,opt perpage=x [,opt results=x [,opt display=x [,opt javascript=false [,opt regex='/pattern/']]]]]]));
: (or) $var=new pageList(array('link'=>'jsfunction("page=#")' [,opt page=x [,opt perpage=x [,opt results=x [,opt display=x [,opt javascript=true [,opt regex='/pattern/']]]]]]));
: The # in the link is the default regex for page numbers. If you include a |, for example, be sure to modify the optional regex variable in order for this to work
Return : $var->firstPage = «« First
: $var->previousPage = « Previous [current-1]
: $var->currentPresent = [[current]
: $var->nextPage = Next »[current+1]
: $var->lastPage = Last »»
: $var->previousPresent = [x] (number shown depends on the current page and 'display' variable)
: $var->nextPresent = [x] (number shown depends on the current page vs total pages and 'display' variable)
: $var->errorMessage = Returns which variable failed. Only present when an error occurs
Notes : The link variable is not escaped. Note that on output the link is enclosed in single quotes (').
: If using javascript links, use double quotes (") to contain your variables.
*/
class pageList {
public function __construct($arrayList) {
$this->defaultConfig=array('page'=>'0','perpage'=>'15','results'=>'15','display'=>'5','link'=>'default','javascript'=>false,'regex'=>'/\#/i');
$this->numberVariables=array('page','perpage','results','display');
if($this->verifyConfig($arrayList)) {
$this->firstPage='«« First'; $this->previousPage='« Previous'; $this->nextPage='Next »'; $this->lastPage='Last »»';
$this->lowerDisplay=(($this->defaultConfig['display']-1)/2); $this->higherDisplay=($this->defaultConfig['display']-1);
$this->numberPages=ceil($this->defaultConfig['results']/$this->defaultConfig['perpage']);
$this->preCap=$this->defaultConfig['page']; if($this->preCap>$this->lowerDisplay) { $this->preCap=$this->lowerDisplay; }
$this->postOffset=($this->higherDisplay-$this->preCap); $this->postOffset=$this->postOffset>(($this->numberPages-1)-$this->defaultConfig['page'])?(($this->numberPages-1)-$this->defaultConfig['page']):$this->postOffset;
$this->javascript=''; if($this->defaultConfig['javascript']==true) { $this->javascript='javascript:'; }
$this->previousPresent=''; $this->nextPresent=''; $this->currentPresent="[javascript.preg_replace($this->defaultConfig[regex],$this->defaultConfig['page'], $this->defaultConfig['link'])."'>".($this->defaultConfig['page']+1)."]";
if($this->defaultConfig['page']>0) {
$this->firstPage="javascript.preg_replace($this->defaultConfig['regex'],"0", $this->defaultConfig['link'])."'>".$this->firstPage."";
$this->previousPage="javascript.preg_replace($this->defaultConfig['regex'],($this->defaultConfig['page']-1), $this->defaultConfig['link'])."'>".$this->previousPage."";
for($i=(($this->defaultConfig['page']-$this->lowerDisplay)-($this->lowerDisplay-$this->postOffset)); $i<$this->defaultConfig['page']; $i++) {
if($i>=0) { $this->previousPresent.="javascript.preg_replace($this->defaultConfig['regex'],$i,$this->defaultConfig['link'])."'>".($i+1)." "; }
}
}
if(($this->numberPages-1)>$this->defaultConfig['page']) {
$this->lastPage="javascript.preg_replace($this->defaultConfig['regex'],($this->numberPages-1), $this->defaultConfig['link'])."'>".$this->lastPage."";
$this->nextPage="javascript.preg_replace($this->defaultConfig['regex'],($this->defaultConfig['page']+1), $this->defaultConfig['link'])."'>".$this->nextPage."";
for($i=($this->defaultConfig['page']+1); $i<=($this->defaultConfig['page']+$this->postOffset); $i++) {
if($i<$this->numberPages) { $this->nextPresent.=" javascript.preg_replace($this->defaultConfig['regex'],$i, $this->defaultConfig['link'])."'>".($i+1).""; }
}
}
}
}
public function verifyConfig($arrayList) {
$this->defaultConfig=array_merge($this->defaultConfig,$arrayList);
foreach($this->defaultConfig as $key => $value) {
if(in_array($key,$this->numberVariables)) { $this->defaultConfig[$key]=intval($this->defaultConfig[$key]); if(is_nan($this->defaultConfig[$key])) { $this->errorMessage=$key.' failed.'; return false; } }
else if($this->defaultConfig[$key]=='default') { $this->errorMessage=$key.' failed.'; return false; }
}
return true;
}
}
Source: http://webdev.livejournal.com/571905.html