原来发表过一个脱离数据库的分页类,最近使用的时候发现有些时候搜索的东西过于大的时候,采用url传递参数的方式,可能会有一定的影响或者叫已知的bug,这次做了一些扩展,同时兼容了以前的模式,使用上面很简单的,只需要多设置一个参数就可以了代码如下:
<?php <BR>/** <BR>* 功能: 分页类,根据提供的数据总量和页面大小 <BR>* 创建日期:Fri Apr 20 16:45:21 CST 2007 <BR>* 最后更新: <BR>* 作者: sanshi <BR>*/ <br><br>class pagination <BR>{ <BR> var $result = array(); <BR> var $pVar = "myp";//page参数分页记数 <BR> var $urlParamStr = ""; //页面的所有参数 <BR> var $sqlMoveNumber = 0; //数据的偏移量 <BR> var $is_post = false; <br><br> public function pagination() <BR> { <br><br> } <BR> public function set($sum,$pageSize,$page="",$is_post = false) <BR> { <BR> $this->is_post = $is_post; <BR> $this->pVar = defined('PAGE_BAR') ? PAGE_BAR : 'myp' ; <BR> $url = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; <BR> //计算出总页数 <BR> $curPage = $this->getCurPage(); <br><br> $temp['sumPage'] = ceil($sum/$pageSize); <BR> //修改了,页码超出的bug <BR> if($curPage>$temp['sumPage']) $curPage=$temp['sumPage']; <BR> $temp['curPage'] = $curPage; <BR> $temp['nextPage'] = $temp['sumPage'] > $curPage ? ($curPage+1) : $temp['sumPage']; <BR> $temp['upPage'] = $curPage >1 ? $curPage-1 :1; <BR> $temp['endPage'] = $temp['sumPage']; <BR> $this->urlParamStr = $this->_makeUrl($url); <BR> $temp['urlPageStr'] = ($page=="") ? $this->_makeUrl($url) : $page.$this->urlParamStr; <br><br> $this->sqlMoveNumber = $curPage <2 ? 0 : ($curPage-1)*$pageSize; <BR> $temp['sqlMoveNumber'] = $this->sqlMoveNumber; <BR> $temp['rowNo'] = $sum; <BR> $this->result = $temp; <BR> return $this->result; <BR> //print_r($temp);exit(); <BR> } <BR> public function getSqlMove() <BR> { <BR> return $this->sqlMoveNumber; <BR> } <BR> public function getFooter() <BR> { <BR> $str= "共有 <FONT COLOR="#FF0033">{$this->result['rowNo']}</FONT> 条 result['urlPageStr']}{$this->result['upPage']}'"; <BR> if($this->is_post) $str .=" onclick='return sanshi_page_post(this.href,{$this->result['upPage']});' "; <BR> $str .=" >上一页 "; <BR> $str .=" 当前 {$this->result['curPage']} 页"; <BR> $str .=" 共有 {$this->result['sumPage']} 页"; <BR> $str .=" result['urlPageStr']}{$this->result['nextPage']}'"; <BR> if($this->is_post) $str .=" onclick='return sanshi_page_post(this.href,{$this->result['nextPage']});' "; <BR> $str .=" >下一页"; <BR> return $str; <BR> } <BR> public function getJumpPage() <BR> { <BR> if($this->is_post) <BR> { <BR> $str = " 跳到第 result['urlPageStr']}\"+this.value,this.value)'>\n"; <BR> }else{ <BR> $str = " 跳到第 result['urlPageStr']}\"+this.value'>\n"; <BR> } <BR>for($i=1;$iresult['sumPage'];$i++) <BR>{ <BR>$str .= ($i==$this->result['curPage']) ? "$i\n" : "$i\n"; <BR>} <BR>$str.=" 页"; <BR> //输出form表单 <BR> if($this->is_post) <BR> { <BR> $str .="<script type="text/javascript"><!---ecms -ecms <BR> function sanshi_page_post(jump_link,page_no) <BR> { <BR> document.getElementById('sanshi_goPage').action = jump_link; document.getElementById('{$this->pVar}').value = page_no; <BR> document.getElementById('sanshi_goPage').submit(); <BR> return false; <BR> } <br><br>// --></script>"; <BR> $str .=""; <BR> $str .="pVar}' id='{$this->pVar}' value=0>"; <BR> $_POST = isset($_POST) ? $_POST : array(); <BR> foreach($_POST AS $k=>$v) <BR> { <BR> $str .=""; <BR> } <BR> $str .=""; <BR> } <BR>return $str; <BR> } <BR> public function getCurPage() <BR> { <BR> return isset($_GET[$this->pVar]) ? intval($_GET[$this->pVar]) : 1 ; <BR> } <BR> //分析出url的参数返回?后的参数,页码为空 包括问号 <BR> private function _makeUrl($url) <BR> { <BR> $arrayUrl=parse_url($url); <BR> if(isset($arrayUrl['query'])) <BR> { <BR> $q=$arrayUrl['query']; <BR> parse_str($q,$qParam); <BR> //print_r($qParam); <BR> if(array_key_exists($this->pVar,$qParam)) <BR> { <BR> foreach ($qParam AS $k=>$v) <BR> { <BR> if($k !=$this->pVar) $temp[$k] = $v; <BR> } <BR> $temp[$this->pVar] = ""; <BR> if(function_exists("http_build_query")) <BR> return '?'.http_build_query($temp); <BR> else <BR> return '?'.sanshi_http_build_query($temp); <BR> }else { <BR> return "?{$q}&{$this->pVar}="; <BR> } <BR> }else{ <BR> return "?{$this->pVar}="; <BR> } <br><br> } <BR>} <BR>function sanshi_http_build_query($a,$b='',$c=0) <BR>{ <BR> if (!is_array($a)) return false; <BR> foreach ((array)$a as $k=>$v) <BR> { <BR> if ($c) <BR> $k=$b."[".$k."]"; <BR> elseif (is_int($k)) <BR> $k=$b.$k; <BR> if (is_array($v)||is_object($v)) <BR> { <BR> $r[]=http_build_query($v,$k,1); <BR> <em style="color:transparent">本文来源gao.dai.ma.com搞@代*码#网</em><a>搞代gaodaima码</a> continue; <BR> } <BR> $r[]=$k."=".urlencode($v); <BR> } <BR> return implode("&",$r); <BR>} <BR>/* <BR>//print_r($_SERVER); <BR>$p = new pagination(); <BR>$p->set(10,2,'thi.php'); <BR>echo $p->getFooter(); <BR>echo $p->getJumpPage(); <BR>*/ <BR>?><BR>