-
- /**************************************
- file: class.paging.php
- php paging class code
- **************************************/
- class paging{
-
- var $sql_results;
- var $sql_query;
-
- var $display_offset;
- var $display_limit;
- var $display_limit_URI;
- var $display_currentpage;
- var $display_totalpages;
- var $alt_content_count;
-
- var $display_offset_var;
- var $display_limit_var;
-
- var $display_mid_offset;
- var $display_link_first;
- var $display_link_prev;
- var $display_link_next;
- var $display_link_last;
-
- var $page_filename;
- var $page_uri;
-
- var $content;
- var $content_type;
- var $content_count;
-
- ## CONSTRUCTOR
- function paging($offsetVar='offset',$limit=10,$midOffset=2){
- $this->display_offset = isset($_REQUEST[$offsetVar])?$_REQUEST[$offsetVar]:0;
- $this->display_limit = is_numeric($limit)?$limit:10;
- $this->display_mid_offset = $midOffset;
- $this->display_offset_var = $offsetVar;
- $this->display_currentpage= ceil($this->display_offset / $this->display_limit);
- }
-
- ## HANDLE DATA
- function setContent($content){
- if( is_array($content) )
- $this->content_type = 'array';
-
- $this->content = $content;
- $this->private_contentDetails();
- $this->getLinks();
- }
-
- function private_contentDetails(){
- if( $this->content_type == 'array' )
- $this->content_count = count($this->content);
- }
-
- function getContent(){
- $returnAry = array();
- for(
- $i=$this->display_offset;
- $i< min( $this->content_count, $this->display_offset+$this->display_limit );
- ++$i
- )
- array_push($returnAry,$this->content[$i]);
-
- return $returnAry;
- }
-
- ## LINKS TO NAVIGATE
- function getLinks(){
- $this->getNextLink('');
- $this->getFirstLink('');
- $this->getLastLink('');
- $this->getPrevLink('');
- }
-
- function getNext(){
- return $this->display_link_next;
- }
-
- function getPrev(){
- return $this->display_link_prev;
- }
-
- function getFirst(){
- return $this->display_link_first;
- }
-
- function getLast(){
- return $this->display_link_last;
- }
-
- function getNextLink($caption){
- // Check if we're counting content or the alternate user provided count
- if( $this->alt_content_count )
- $count = $this->alt_content_count;
- else
- $count = $this->content_count;
-
- if( $this->display_offset+$this->display_limit-$count >=0 ){
-
- $this->display_link_next = $this->display_offset;
- }else{
- $this->display_link_next = min(
- $count-1,
- $this->display_offset+$this->display_limit
- );
- }
- return $this->buildLink( $this->buildNewURI( $this->display_link_next), $caption );
- }
-
- function getPrevLink($caption){
- $this->display_link_prev = max(
- 0,
- $this->display_offset-$this->display_limit
- );
- return $this->buildLink( $this->buildNewURI( $this->display_link_prev), $caption );
- }
-
- function getFirstLink($caption){
- $this->display_link_first = 0;
- return $this->buildLink( $this->buildNewURI( $this->display_link_first), $caption );
- }
-
- function getLastLink($caption){
- $this->display_link_last = $this->content_count-$this->display_limit;
- return $this->buildLink( $this->buildNewURI( $this->display_link_last), $caption );
- }
-
- ## NUMBERS
- function getPageCount(){
- if( $this->alt_content_count )
- $count = $this->alt_content_count;
- else
- $count = $this->content_count;
-
- $this->display_totalpages = ceil(
- $count / $this->display_limit
- );
- return $this->display_totalpages;
- }
-
- function getPageNumbers(){
-
- // define variables
- $midOffset = $this->display_mid_offset;
- $firstLink = $this->display_link_first;
- $pageCount = $this->getPageCount();
- $thisPage = $this->display_currentpage;
- $limit = $this->display_limit;
- $displayed=$midOffset*2+1;
- $returnAry=array();
-
- // dots
- $returnAry['afterdot'] = '';
- $returnAry['beforedot'] = '';
-
-
- // if two times and center the number is less than all the pages together
- if( ($midOffset*2+1) < $pageCount ){
- $min = max($firstLink,$thisPage-$midOffset);
- $max = min($pageCount,$thisPage+$midOffset+1);
- $total = $max-$min;
-
- // this keeps x amount of pages displayed even if
- // you're not in mid cause of page 1 or 2
- if($total<$displayed){
-
- if($min==0){
- $max+=$displayed-$total;
- }
- if($max==$pageCount){
- $min-=$displayed-$total;
- }
- }
-
- }else{
- # Just output a set of numbers
- $min = 0;
- $max = $pageCount;
- }
-
- // run pages, check for current page and name it
- for($i=$min;$i<$max;++$i){
- $cp = 'no';
- if($thisPage==$i)
- $cp = 'yes';
-
- if($max!=$pageCount)
- $returnAry['afterdot'] = '...';
-
- if($min>0)
- $returnAry['beforedot'] = '...';
-
- array_push($returnAry,
- array(
- 'currentPage'=>$cp,
- 'pageNumber'=>($i+1),
- 'pageLink'=>$this->buildLink( $this->buildNewURI( ($i*$limit) ), ($i+1) )
- )
- );
- }
-
- return $returnAry;
-
- }
-
- function makePageNumbers($format, $pages,$boldCurrent=true,$separator=' '){
- $retPages = '';
-
- // make actual page numbers
- foreach($pages as $key => $value):
- if(is_numeric($key))
- $retPages .= ('yes'==$value['currentPage'] && $boldCurrent)?''.$value['pageLink'] .''.$separator:$value['pageLink'].$separator;
- endforeach;
-
- $format = str_replace( array('{beforedot}',
- '{afterdot}',
- '{pages}',
- '{separator}'),
- array( $pages['beforedot'],
- $pages['afterdot'],
- $retPages),
- $format);
- return $format;
- }
-
- ## CHECKS
- function isFirstPage(){
- if($this->display_currentpage==0)
- return true;
- return false;
- }
- function isLastPage(){
- // add one because basis is 0, not 1
- if($this->display_currentpage+1==$this->getPageCount())
- return true;
- return false;
- }
-
- ## FUNCTIONS
- function getPageName(){
- $fileName = explode('/',$_SERVER['REQUEST_URI']);
- $fileName = $fileName[count($fileName)-1];
-
- if(strpos($fileName,'?')>0) {
- $fileName = explode('?',$fileName);
- $fileName = $fileName[0];
- }
-
- return $fileName;
- }
-
- function getCleanURI(){
- $URI = $_SERVER['REQUEST_URI'];
- if(strpos($URI,'?')>0){
- $URI = explode('?',$URI);
- $URI = $URI[1];
-
- $URI = preg_replace('/b'.$this->display_offset_var.'b[=0-9&]{2,20}/','',$URI);
- //$URI = preg_replace('/b'.$this->display_limit_var.'b[=0-9&]{2,20}/','',$URI);
-
- return $URI;
- }
- return false;
- }
-
- function buildNewURI($offset){
- $newFile = $this->getPageName() . '?' . $this->getCleanURI();
-
- $lastChar = substr($newFile,strlen($newFile)-1,1);
- if( $lastChar != '&' && $lastChar != '?' )
- $newFile.='&';
-
- $newFile .= $this->display_offset_var.'='.$offset.'&';
- //$newFile .= $this->display_limit_var.'='.$limit.'&';
-
- return $newFile;
- }
-
- function buildLink( $href, $caption, $target=NULL ){
- if( $target != NULL )
- $target = ' target="'.$target.'"';
- return ''.$caption.'';
- }
-
- function encodeURI($str){
- $salt = 'falaful';
- $str = strrev($salt.$str);
- return base64_encode($str);
- }
-
- function decodeURI($str){
- $salt = 'falaful';
- $str = strrev( base64_decode($str) );
- $str = substr( $str, strlen($salt), strlen($str) );
- return $str;
- }
-
- ##############
- #
- #These functions are for inputting a query for this
- #class to execute. The other functions will grab all
- #x amount of rows then truncate it. These functions will
- #only limit to whatever amount. This improves performance.
- #Reason is so you dont grab 1,000,000 rows when you want 3.
- #
- ##############
-
- function runWQuery($db, $statement, $table, $where=''){
-
- // get total rows
- $db->query( 'SELECT COUNT(*) AS count FROM '. $table . ' ' . $where );
- $db->movenext();
- $this->alt_content_count = $db->col['count'];
-
- // add limit to query
- $where .= ' LIMIT ' . $this->display_offset .', '.$this->display_limit;
-
- // save query
- $this->sql_query = $statement . ' FROM ' . $table .' '. $where;
-
- // print query
- //echo $this->sql_query;
-
- // run query
- $db->query( $this->sql_query );
- $this->sql_results = array();
-
- // save results
- while($db->movenext()){
- array_push($this->sql_results , $db->col);
- }
-
- return $this->runQueryActions();
- }
-
- function runQueryActions(){
- $this->setContent( $this->sql_results );
-
- $pages = $this->getPageNumbers();
-
- // make actual page numbers
- $retPages = $this->makePageNumbers( '{beforedot} {pages} {afterdot}', $pages, true, ' ' );
-
- // get new display
- return array(
- 'content'=>$this->sql_results,
- 'pages'=>$retPages
- );
- }
- }
- ?>
复制代码
|