Location: PHPKode > scripts > eoPaginator > eoPaginator.php
<?php
    /*
     * @class		eoPaginator
     * @description	Class for page links bar output with shift|slide|intersection modes.
     * @version		1.0.1
     */
     
class eoPaginator
{
    public $max_pages = 10;	/* Maximum amount of displayed page links  (int) */
    public $rec_per_page = 10;	/* Records per one page (int) */
    
    public $mode = 1;		/* 1-shift mode, 2-slide mode, 3-intersection mode (int) */
    
    public $sess_name;		/* ONLY FOR INTERSECTION MODE: Name of session pages segment stored in (string) */
    public $overlap = 2;	/* ONLY FOR INTERSECTION MODE: Pages segments ovelapping. Cant be equal or more then
				    $max_pages value !  (int) */
    
    public $a_href = "#";	/* URL to define <a href= > NOTE! "_PAGNUM_" pattern will be replaced by page number (string) */	
    public $a_onclick;		/* OnClick event for <a> element NOTE! "_PAGNUM_" pattern will be replaced by page number (string) */
    public $a_class;		/* CSS class of an <a> element (string) */
    public $a_sel_class;	/* CSS class of the selected <a> element (string) */
    
    public $left_arrow_sym = "&lt;";	/* Symbol for left arrow (string) */
    public $right_arrow_sym = "&gt;";	/* Symbolfor right arrow (string) */

    public $query_limit;	/* Returned SQL-query LIMIT substring (string) */
    
    const PAT = "/_PAGNUM_/";	/* Pattern wich will be replaced by actual page number */
    
    public $all_pages;		/* This variable contain total number of pages*/
    
    
    public function make_pgr($rec_total,$cur_page=1)
    {
	$block=array();
	$this->all_pages=ceil($rec_total/$this->rec_per_page);
	    if ($this->all_pages==1) return; /* If number of pages = 1 everething else have no sence, so return */
	
	/* Definition of an << and < elements (satrt page & previous page)
	 * << - go to first page
	 *  < - go to previous page
	 */
	    if ($cur_page>1)
	    {
		$to_first="<a class=\"".$this->a_class."\" href=\"".preg_replace(self::PAT,"1",$this->a_href)."\" 
			    onclick=\"".preg_replace(self::PAT,"1",$this->a_onclick)."\">".
			    $this->left_arrow_sym.$this->left_arrow_sym."</a> ";
		    $prev=$cur_page-1;
		$to_prev="<a class=\"".$this->a_class."\" href=\"".preg_replace(self::PAT,$prev,$this->a_href)."\" 
			    onclick=\"".preg_replace(self::PAT,$prev,$this->a_onclick)."\">".$this->left_arrow_sym."</a> ";
	    } else { $to_first=""; $to_prev=""; }
	
	    
	/* Definition of an > and >> elements (next page & last page)
	 *  > - go to next page
	 * >> - go to last page
	 */	 
	    if ($cur_page<$this->all_pages)
	    {
	    	    $next=$cur_page+1;
		$to_next="<a class=\"".$this->a_class."\" href=\"".preg_replace(self::PAT,$next,$this->a_href)."\" 
			    onclick=\"".preg_replace(self::PAT,$next,$this->a_onclick)."\">".$this->right_arrow_sym."</a> ";
		$to_last="<a class=\"".$this->a_class."\" href=\"".preg_replace(self::PAT,$this->all_pages,$this->a_href)."\" 
			    onclick=\"".preg_replace(self::PAT,$this->all_pages,$this->a_onclick)."\">".
			    $this->right_arrow_sym.$this->right_arrow_sym."</a> ";
	    } else { $to_next=""; $to_last=""; }
	    
	
	/* Definition of page diapason edges */
	if ($this->all_pages<=$this->max_pages)
	    {$eges=array(1,$this->all_pages);}
	else
	    {
		switch ($this->mode)
		{
		    case "1":
			$eges=$this->getShiftSegment($cur_page);
			break;
		    case "2":
			$eges=$this->getSlideSegment($cur_page);
			break;	
		    case "3":
			$eges=$this->getIntersectSegment($cur_page);
			break;
		}
		
	    }
	    
	    /* Make array of links */
	    $block[0]=$to_first;  $block[1]=$to_prev;
	    for ($i=$eges[0];$i<=$eges[1];$i++)
		{
	    	    if ($i==$cur_page)
		    { $class=$this->a_sel_class; } else { $class=$this->a_class; }
		    
		    array_push($block,"<a class=\"".$class."\" href=\"".preg_replace(self::PAT,$i,$this->a_href)."\" 
					    onclick=\"".preg_replace(self::PAT,$i,$this->a_onclick)."\">".$i."</a> ");
		}
	    array_push($block,$to_next,$to_last);
	    /* --- */
	$this->getQueryLimit($cur_page);    
	return $block;        
    }

    private function getShiftSegment($cur_page=1)
    {
	$seg_start = ((int)(($cur_page-1)/$this->max_pages))*$this->max_pages+1;
	$eges = array($seg_start,min($seg_start+$this->max_pages-1,$this->all_pages));	
	return $eges;
    }
    
    private function getSlideSegment($cur_page=1)
    {
	$isodd = $this->max_pages & 1;
	switch ($isodd)
	{
	    case 1: // In case when $max_pages is odd
		$delta=$this->max_pages-(ceil($this->max_pages/2));
		if ($cur_page>$delta)
		{ $edges = array($cur_page-$delta,min($cur_page+$delta, $this->all_pages)); }
		else { $edges = array(1,min($this->max_pages, $this->all_pages)); }
		break;

	    case 0: // In case when $max_pages is even
		$delta_left=$this->max_pages/2;
		$delta_right=($this->max_pages/2)-1;
		if ($cur_page>$delta_left)
		{ $edges = array($cur_page-$delta_left,min($cur_page+$delta_right, $this->all_pages)); }
		else { $edges = array(1,min($this->max_pages, $this->all_pages)); }
		break;
	}
	return $edges;	
    }
    
    private function getIntersectSegment($cur_page=1)
    {
	if (!$this->checkOverlapValidity()) return;
	session_name($this->sess_name);
	session_start();
	    $seg_num = isset($_SESSION["seg_num"]) ? $_SESSION["seg_num"] : 1;
		if ($cur_page==1) $seg_num=1;
		if ($cur_page==$this->all_pages) $seg_num=$this->getIntersectLastSeg();
	    $lim=$this->getIntersectLim($seg_num);	    
	    if (($cur_page<>$this->all_pages) && ($cur_page==$lim[1])) $seg_num++;
	    if (($cur_page<>1) && ($cur_page==$lim[0])) $seg_num--;
	    $lim=$this->getIntersectLim($seg_num);
	    $_SESSION["seg_num"]=$seg_num;
	    return $lim;
    }
    
    private function getIntersectLim($seg_num=1)
    {	
	
	$lim[0]=$seg_num*$this->max_pages-(($this->max_pages-1)+(($seg_num-1)*$this->overlap));
	$lim[1]=min($lim[0]+($this->max_pages-1),$this->all_pages);
	return $lim;
    }
    
    private function getIntersectLastSeg()
    {
	$last = 1+ceil(($this->all_pages-$this->max_pages)/($this->max_pages-$this->overlap));
	return $last;
    }
    
    private function getQueryLimit($cur_page=1)
    {
	$start = (($cur_page-1)*$this->rec_per_page)+1;
	$this->query_limit = "LIMIT ".$start.", ".$this->rec_per_page;
	return;	
    }

    /*
       This function check overlap value validity for intersection mode case. Overlap value CAN'T be >=
       maximum pages number according to common logic.
    */       
    private function checkOverlapValidity()
    {
	if ($this->overlap>=$this->max_pages) 
	{
	    trigger_error("Overlap value can`t be equal or more then max_page value!",E_USER_ERROR);
	    return false;
	}
	return true;	    
    }
}
?>
Return current item: eoPaginator