Location: PHPKode > scripts > Pagerizer > pagerizer/pagerizer.class.php
<?php
/******************************************

Pagerizer Class
-------------------------------------------
With litte to no configuration, this class
will display links to numbered pages or
result sets.
-------------------------------------------

Copyright (c) 2008, Bob Kennedy
hide@address.com

You are free to edit and redistribute this
class as you see fit as long as the
copyright statement above if left intact.
If you like it, let me know!

******************************************/

class pagerizer {
	
	public $current=false;
	public $start_number=1;
	public $base_url=false;
	public $real_url=false;
	public $next=">>";
	public $prev="<<";
	public $threshold=7;
	public $pad_current=2;
	public $show_every=10;
	public $elipses="...";
	public $link_class='page_link';
	public $current_page_class='pagerizer_current_page';
	public $elipses_class='pagerizer_elipses';
	public $span_class='pagerizer_span';
	public $container_div_class=false;
	public $qs=false;
	public $pagerizer_get_key="pgrzr";
	
	//pseudo constructor function
	function pagerizer($total, $c=false, $current=false){
		//initialize values
		$this->total_count=$total;
		$this->base_url=$_SERVER['SCRIPT_NAME'];
		$this->real_url=$_SERVER['SCRIPT_NAME'];
		$this->qs=$_SERVER['QUERY_STRING'];
		//use config options if set
		if(isset($c['start_number']) and $c['start_number']!==false)$this->start_number=$c['start_number'];
		if($current==false)$this->current=$this->start_number;
		if(isset($c['base_url']) and $c['base_url']!==false)$this->base_url=$c['base_url'];
		if(isset($c['next']) and $c['next']!==false)$this->next=$c['next'];
		if(isset($c['prev']) and $c['prev']!==false)$this->prev=$c['prev'];
		if(isset($c['threshold']) and $c['threshold']!==false)$this->threshold=$c['threshold'];
		if(isset($c['pad_current']) and $c['pad_current']!==false)$this->pad_current=$c['pad_current'];
		if(isset($c['show_every']) and $c['show_every']!==false)$this->show_every=$c['show_every'];
		if(isset($c['elipses']) and $c['elipses']!==false)$this->elipses=$c['elipses'];
		if(isset($c['link_class']) and $c['link_class']!==false)$this->link_class=$c['link_class'];
		if(isset($c['current_page_class']) and $c['current_page_class']!==false)$this->current_page_class=$c['current_page_class'];
		if(isset($c['elipses_class']) and $c['elipses_class']!==false)$this->elipses_class=$c['elipses_class'];
		if(isset($c['span_class']) and $c['span_class']!==false)$this->span_class=$c['span_class'];
		if(isset($c['container_div_class']) and $c['container_div_class']!==false)$this->container_div_class=$c['container_div_class'];
		if(isset($c['pgrzr_get_key']) and $c['pgrzr_get_key']!==false)$this->pagerizer_get_key=$c['pgrzr_get_key'];
		if(isset($_GET[$this->pagerizer_get_key]) and is_numeric($_GET[$this->pagerizer_get_key]))$this->current=$_GET[$this->pagerizer_get_key];
		
		//use all page get arguments except for the current page
		//real_url is the current url, minus the page number
		if(strlen($this->qs)>0){
			$all=explode("&",$this->qs);
			$this->real_url=$this->base_url."?";
			foreach($all as $arg){
				list($k,$v)=explode("=",$arg);
				if($k!=$this->pagerizer_get_key){
					$arglist.=$k."=".$v."&";
				}
			}
			$this->real_url.=$arglist;
		}else{
			$this->real_url=$this->base_url."?";
		}
		$this->real_url.=$this->pagerizer_get_key."=";
	}

	function display(){
		//show the links
		$e=false;
		if($this->container_div_class)$ret="<div class='".$this->container_div_class."'>";
		//the "previous" link
		if($this->start_number!=$this->current and $this->prev)$ret.="<span class='".$this->span_class."'><a class='".$this->link_class."' href='".$this->real_url.($this->current-1)."'>".$this->prev."</a></span>";
		for($x=0, $y=$this->start_number;$x<$this->total_count;$x++, $y++){
			if($y==$this->current){
				//this is the page we're on
				$ret.="<span class='".$this->current_page_class."'>".$y."</span>";
				$e=false;
			}elseif($this->total_count<=$this->threshold or ($y%$this->show_every==0) or ($y>=($this->current-$this->pad_current) and $y<=($this->current+$this->pad_current))){
				//this is every link except padding links around current
				$ret.="<span class='".$this->span_class."'><a class='".$this->link_class."' href='".$this->real_url.$y."'>".$y."</a></span>";
				$e=false;
			}elseif($this->total_count>$this->threshold and ($y==$this->current or $y==$this->current-1 or $y==$this->current+1 or $y==$this->start_number or $x==$this->total_count-1)){
				//this is padding links around current
				$ret.="<span class='".$this->span_class."'><a class='".$this->link_class."' href='".$this->real_url.$y."'>".$y."</a></span>";
				$e=false;
			}elseif(!$e){
				//this is the elipses
				$e=true;
				$ret.="<span class='".$this->elipses_class."'>".$this->elipses."</span>";
			}
		}
		//the "next" link
		if($this->current!=$this->total_count-1+$this->start_number and $this->next)$ret.="<span class='".$this->span_class."'><a class='".$this->link_class."' href='".$this->real_url.($this->current+1)."'>".$this->next."</a></span>";
		if($this->container_div_class)$ret.="</div>";
		echo $ret;
	
	}
	function show_style($style=false){
		//Outputs a stylesheet for the current class names, with comments to describe each one and some basic styles.
		if($style==1)echo "<style>";
		echo ".".$this->link_class."{
		/* each linked page uses this class*/
			color:#555;
			text-decoration:none;
			font-weight:bold;
		}
		.".$this->current_page_class."{
		/* the page you are on uses this class*/
			color:white; 
			font-weight:bold; 
			background:#aaa; 
			border:1px solid #999; 
			padding:2px; 
			margin:1px; 
			line-height:25px; 
			font-size:12px;
		}
		.".$this->elipses_class."{
		/* the elipses uses this class*/
			color:black; 
			font-weight:bold; 
			background:#fff; 
			padding:2px; 
			margin:1px; 
			line-height:25px; 
			font-size:12px;
		}
		.".$this->span_class."{
		/* every link has a span around it with this class*/
			background:#eee; 
			border:1px solid #999; 
			padding:2px; 
			margin:1px; 
			line-height:25px; 
			font-size:12px;
		}
		";
		if($this->container_div_class)echo".".$this->container_div_class."{
		/* this is the class of the div around the whole thing */
			border:1px solid #777;
			padding:5px;
			display:inline;
		}
		";
		if($style==1)echo "</style>";
	}
}
?>
Return current item: Pagerizer