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?
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) == '?') ? '&' : '?' ; 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>','»','First','Last'); echo $pager->get_prev('<a href="{LINK_HREF}" title="Previous">«</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">»</a>'); echo $pager->get_title('Results {FROM} to {TO} of {TOTAL}<br />'); ?> PHP:
Thanks. i was also able to find this tutorial, a little simpler http://www.php-mysql-tutorial.com/php-mysql-paging.php