您好,欢迎访问一九零五行业门户网

经典php分页类代码

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 datafunction 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; $icontent_count, $this->display_offset+$this->display_limit ); ++$i)array_push($returnary,$this->content[$i]); return $returnary;} ## links to navigatefunction 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 countif( $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 );} ## numbersfunction 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 togetherif( ($midoffset*2+1) $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 2if($total 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 itfor($i=$min;$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 numbersforeach($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;} ## checksfunction isfirstpage(){if($this->display_currentpage==0)return true;return false;}function islastpage(){// add one because basis is 0, not 1if($this->display_currentpage+1==$this->getpagecount())return true;return false;} ## functionsfunction 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 resultswhile($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 displayreturn array('content'=>$this->sql_results,'pages'=>$retpages);}} ?>
复制代码
其它类似信息

推荐信息