Location: PHPKode > projects > igloo opensource > xmods/simbio/GUI/paging/GUI_paging.inc.php
<?php

/*-------1---------2---------3---------4---------5---------6---------7---------8---*/
// +----------------------------------------------------------------------+
// | PHP version 4, 5                                                     |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The YHA (Yono Hendro Arie) Group             |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at                              |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | hide@address.com so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Symbio Paging Class                                                  |
// | Generate a pager set for db resultset paging                         |
// +----------------------------------------------------------------------+
// | Authors: Arie Nugraha <hide@address.com>                            |
// +----------------------------------------------------------------------+
//
// $Id$

# global variables

class GUI_paging
{
    # properties
    var $all_recs_num = 0;
    var $recs_each_page = 0;
    var $_pages_each_set = 0;
    
    # static method pager set
    # no need to make an object from this class
    # to use just call : echo GUI_paging::pagerSet(200, 20, 10);
    /*
    @return string - string of pager set
    @param integer $all_recs_num - total number data retrieved
    @param integer $recs_each_page - number of record to show in each page
    @param integer $_pages_each_set - number of page link to show in each pager set
    */
    function pagerSet($all_recs_num, $recs_each_page, $pages_each_set = 10){
    	// put the argument into class properties
        $this->all_recs_num = (integer)$all_recs_num;
        $this->recs_each_page = (integer)$recs_each_page;
        $this->pages_each_set = (integer)$pages_each_set;
        
        // total number of pages
        $_num_page = ceil($this->all_recs_num/$this->recs_each_page);
    	
        if ($_num_page <= 1) {
    		return false;
    	}
    	
        // total number of pager set
    	$_pager_set_num = ceil($_num_page/$this->pages_each_set);
    	
    	if (isset($_GET['pagerset']) AND $_GET['pagerset'] > 1) {
    		$_pager_set = (integer)$_GET['pagerset'];
    		$_pager_offset = (($_pager_set*$this->pages_each_set) - $this->pages_each_set) + 1;
    	} else {
    		$_pager_set = 1;
    		$_pager_offset = 1;
    	}	
    	
    	// check the current page number
    	if (isset($_GET["page"]) AND $_GET["page"] > 1) {
    		$_page = (integer)$_GET["page"];
    	} else {$_page = 1;}
    	
    	// check the query string
    	if(isset($_SERVER['QUERY_STRING']) AND !empty($_SERVER['QUERY_STRING'])){
    		// check if there is already GET variables and then strip it with Regex
    		if (preg_match("/&page=/i", $_SERVER['QUERY_STRING'])) {
    			$_query_str_page = preg_replace("/&page=[1-9]+|&pagerSet=[1-9]+/i", '', $_SERVER['QUERY_STRING']);
    			$_query_str_page = "?$_query_str_page&page=";
    		} elseif (preg_match("/^page=/i", $_SERVER['QUERY_STRING'])) {
    			$_query_str_page = '?page=';
    		} else {$_query_str_page = '?'.$_SERVER['QUERY_STRING'].'&page=';}

    		$_current_page = $_SERVER['PHP_SELF'].$_query_str_page;
    	} else {$_current_page = $_SERVER['PHP_SELF'].'?page=';}

    	// init the return string
    	$_buffer = "<table cellpadding='5' align='center'>\n";
    	$_buffer .= "<tr>\n";
    	// integer to limit the pager
    	$_limit = 1;
        
        // make a previous link if we are in pager set
    	if ($_pager_set > 1) {
    		$_buffer .= "<td align='center'>";
            $_buffer .= "<a href='$_current_page".($_pager_offset - 1)."&pagerset=".($_pager_set - 1)."'>Previous</a></td>\n";
    	}
    	
    	for ($p = $_pager_offset; $p <= $_num_page AND $_limit <= $this->pages_each_set; $p++) {
    		if ($p == $_page) {
    			$_buffer .= "<td align='center'><b>$p</b></td>\n";
    		} else {
    			$_buffer .= "<td align='center'>";
    			$_buffer .= "<a href='$_current_page".$p."&pagerset=$_pager_set'>$p</a></td>\n";
    		}
    		
            $_limit++;
    	}
    	
    	// make a next link button
    	if ($_pager_set > 1 AND $_pager_set < $_pager_set_num) {
    		$_buffer .= "<td align='center'>";
            $_buffer .= "<a href='$_current_page".($_pager_offset + $this->pages_each_set)."&pagerset=".($_pager_set + 1)."'>Next</a></td>\n";
        } elseif ($_pager_set == 1 AND $_pager_set < $_pager_set_num) {
    		$_buffer .= "<td align='center'>";
            $_buffer .= "<a href='$_current_page".($this->pages_each_set + 1)."&pagerset=".($_pager_set + 1)."'>Next</a></td>";
    	} elseif ($_pager_set == $_pager_set_num) {
    		$_buffer .= '';
    	}
    	
    	$_buffer .= "</tr>\n";
    	$_buffer .= "</table>\n";
    	
    	return $_buffer;
    } 
}
# End of GUI_paging class declaration

?>
Return current item: igloo opensource