how to do nicer paging? (like in this forum)

Discussion in 'PHP' started by frankcow, Apr 25, 2007.

  1. #1
    I'm working on a page that displays all records in pages. The issue is that when I have a few hundred or thousand records, the number of pages is absolutely ridiculous.

    Does anyone know where I can find some code or tutorial on making a simplified paging system, maybe only showing the next few results?
     
    frankcow, Apr 25, 2007 IP
  2. dzysyak

    dzysyak Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you should be able to findsome pager code on phpclasses[dot]org
     
    dzysyak, Apr 25, 2007 IP
    frankcow likes this.
  3. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #3
    maybe this can help you :

    http://www.firepages.com.au/php_pagination_class.htm


    <?php 
    /** 
    * firepages.com.au - basic pagination class - see 
    * firepages.com.au/php_pagination.htm for more information 
    */ 
    class pager{ 
        var $p_range    = 0; # range to show if you dont want to show ALL pages returned 
        var $curr       = 1;    # current page number 
        var $_pages  = '';     # no of pages in a recordset 
        var $_ctl       = '_p';  # default control variable name 
        var $_req_url ='';      # url to build links with 
        var $_req_qs ='';      # query string to build links with 
        # allowed replacements for titles and links 
        var $_t_tpls   = array('{CURRENT}','{FROM}','{TO}','{MAX}','{TOTAL}'); 
        var $_l_tpls   = array('{LINK_HREF}','{LINK_LINK}'); 
    
        function pager($max, $pp, $curr, $extra='') 
        { 
            if(!is_array($extra)){ 
                $extra=array(); 
            } 
            $this->_pp       = $pp; 
            $this->curr       = (int)$curr > 0 ? $curr  : 1 ; 
            $this->_pages  = $this->p_range = ceil( $max/$pp ); 
            $this->_ctl     .= empty($extra['suffix']) ? '' : $extra['suffix'] ; 
            $this->_req_qs = isset($extra['query_string']) ? 
                $extra['query_string'] : $_SERVER['QUERY_STRING'] ; 
            $this->_req_url = isset($extra['php_self']) ? 
                $extra['php_self'] : $_SERVER['PHP_SELF'] ; 
    
        #check for and remove control variables from query string# 
            if(strpos($this->_req_qs,$this->_ctl)!==false){ 
                parse_str($this->_req_qs,$arr); 
                $tmp=array(); 
                unset($arr[$this->_ctl]); 
                    foreach($arr as $k=>$v){ 
                        $tmp[]="$k=$v"; 
                    } 
                    $this->_req_qs = implode('&', $tmp); 
                    unset($tmp); 
            } 
            #vars for eye_candy not declared ~# 
            $this->_from = (($this->curr * $this->_pp) - $this->_pp) + 1; 
            $to               = ($this->_from +  $this->_pp) -1 ; 
            $this->_to     = ($to > $max ) ? $max : $to ; 
            $this->_total = $max ; 
        } 
    
        function set_range($p_range) 
        { 
            $this->p_range = $p_range; 
        } 
    
        function get_limit() 
        { 
            return ($this->curr * $this->_pp) - $this->_pp. ' , '.$this->_pp; 
        } 
    
        function get_limit_offset() 
        { 
            return ($this->curr * $this->_pp) - $this->_pp; 
        } 
    
        function get_title($format) 
        { 
            return str_replace($this->_t_tpls, 
                array($this->curr, $this->_from, $this->_to, $this->_pages, $this->_total), $format); 
        } 
    
        function _get_qurl() 
        { 
            $q = empty($this->_req_qs) ? '' : '?'.$this->_req_qs ; 
            $s = (substr($q, 0, 1) == '?') ? '&amp;' : '?' ; 
            return $this->_req_url . $q . $s . $this->_ctl . '='; 
        } 
    
        function get_prev($format) 
        { 
            return $this->curr > 1 ? 
                str_replace($this->_l_tpls,array($this->_get_qurl().($this->curr -1)),$format) : '' ; 
        } 
    
        function get_next($format) 
        { 
            return ($this->curr < $this->_pages) ? 
                str_replace($this->_l_tpls,array($this->_get_qurl().($this->curr +1)),$format) : '' ; 
        } 
    
        function get_range($format, $sep,$first='',$last='') 
        { 
            if($this->_pages < 2){ 
                return ; 
            } 
            $pre_url = $this->_get_qurl(); 
            $lfirst = $llast = '' ; 
            $min  = 1 ; 
            $to = $this->_pages ; 
    
            if($this->_pages > $this->p_range){ 
                $mid = ceil(($this->p_range / 2)); 
                if(($this->curr - $mid) >= 1){ 
                    $min = $this->curr - $mid; 
                } 
                $to = $min + ($this->p_range-1); 
                if($this->_pages > $to){ 
                    $llast = (!empty($last)) ? 
                               $sep.str_replace($this->_l_tpls,array($pre_url.$this->_pages,$last),$format) : '' ; 
                } 
               if($min > 1){ 
                   $lfirst = (!empty($first) && $this->curr >1 ) ? 
                               str_replace($this->_l_tpls,array($pre_url.'1',$first),$format) .$sep : '' ; 
               } 
               if($to > $this->_pages){ 
                   $to = $this->_pages ; 
               } 
            } 
            for($x=$min; $x<=$to; ++$x){ 
                $rets[]=($this->curr!=$x)?str_replace($this->_l_tpls, array($pre_url.$x, $x) , $format):$x; 
            } 
            return $lfirst.implode($sep, $rets).$llast; 
        } 
    } 
    ?> 
    PHP:

    Usage :


    <? 
    include_once 'pager.class.php'; 
    #get the maximum number of results 
    $max = mysql_result( 
        mysql_query("SELECT COUNT(id) FROM colours WHERE id > 0"), 
        0 ) ; 
    
    #construct the pager , here showing 8 results per page 
    $pager  = new pager($max, 8, @$_GET['_p']); 
    
    #set the amount of pages to show at any one time 
    $pager->set_range(6); 
    
    #the real query 
    $sql = "SELECT id,colour_name,hex_value FROM colours WHERE id > 0 LIMIT ".$pager->get_limit() ; 
    $q = mysql_query($sql) ; 
    
    #display stuff 
    echo $pager->get_title('Hex Colours :: Page {CURRENT} of {MAX}<br />'); 
    echo $pager->get_range('<a href="{LINK_HREF}">{LINK_LINK}</a>','&raquo;','First','Last'); 
    echo $pager->get_prev('<a href="{LINK_HREF}" title="Previous">&laquo;</a>'); 
    while($r = mysql_fetch_assoc($q)){ 
        echo "<div> #{$r['hex_value']}</div>"; 
    } 
    echo $pager->get_next('<a href="{LINK_HREF}" class="ex_head" title="Next">&raquo;</a>'); 
    echo $pager->get_title('Results {FROM} to {TO} of {TOTAL}<br />'); 
    ?> 
    
    PHP:
     
    commandos, Apr 25, 2007 IP
    frankcow likes this.
  4. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #4
    frankcow, Apr 25, 2007 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    Barti1987, Apr 25, 2007 IP