html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

php分页代码实例_PHP教程_编程技术

[ ] 已经帮助:人解决问题
  1. function pagestring($count, $pagesize, $wap=false) {  
  2.     $p = new Pager($count, $pagesize, 'page');  
  3.     if ($wap) {  
  4.         return array($pagesize, $p->offset, $p->genWap());  
  5.     }  
  6.     return array($pagesize, $p->offset, $p->genBasic());  
  7. }  
  8.  
  9.  
  10. pagestring文件  
  11.  
  12. <?php 
  13.  
  14. class Pager{  
  15.  
  16.     public $rowCount = 0;  
  17.     public $pageNo = 1;  
  18.     public $pageSize = 20;  
  19.     public $pageCount = 0;  
  20.     public $offset = 0;  
  21.     public $pageString = 'page';  
  22.  
  23.     private $script = null;  
  24.     private $valueArray = array();  
  25.  
  26.     public function __construct($count=0, $size=20, $string='page')  
  27.     {  
  28.         $this->defaultQuery();  
  29.         $this->pageString = $string;  
  30.         $this->pageSize = abs($size);  
  31.         $this->rowCount = abs($count);  
  32.  
  33.         $this->pageCount = ceil($this->rowCount/$this->pageSize);  
  34.         $this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;  
  35.         $this->pageNo = abs(intval(@$_GET[$this->pageString]));  
  36.         $this->pageNo = $this->pageNo==0 ? 1 : $this->pageNo;  
  37.         $this->pageNo = $this->pageNo>$this->pageCount   
  38.             ? $this->pageCount : $this->pageNo;  
  39.         $this->offset = ( $this->pageNo - 1 ) * $this->pageSize;  
  40.     }  
  41.  
  42.     private function genURL( $param, $value ){  
  43.         $valueArray = $this->valueArray;  
  44.         $valueArray[$param] = $value;  
  45.         return $this->script . '?' . http_build_query($valueArray);  
  46.     }  
  47.  
  48.     private function defaultQuery()  
  49.     {  
  50.         ($script_uri = @$_SERVER['SCRIPT_URI'])  ($script_uri = @$_SERVER['REQUEST_URI']);  
  51.         $q_pos = strpos($script_uri,'?');  
  52.         if ( $q_pos > 0 )  
  53.         {  
  54.             $qstring = substr($script_uri, $q_pos+1);  
  55.             parse_str($qstring, $valueArray);  
  56.             $script = substr($script_uri,0,$q_pos);  
  57.         }  
  58.         else  
  59.         {  
  60.             $script = $script_uri;  
  61.             $valueArray = array();  
  62.         }  
  63.         $this->valueArray = empty($valueArray) ? array() : $valueArray;  
  64.         $this->script = $script;  
  65.     }  
  66.  
  67.     public function paginate($switch=1){  
  68.         $from = $this->pageSize*($this->pageNo-1)+1;  
  69.         $from = ($from>$this->rowCount) ? $this->rowCount : $from;  
  70.         $to = $this->pageNo * $this->pageSize;  
  71.         $to = ($to>$this->rowCount) ? $this->rowCount : $to;  
  72.         $size = $this->pageSize;  
  73.         $no = $this->pageNo;  
  74.         $max = $this->pageCount;  
  75.         $total = $this->rowCount;  
  76.  
  77.         return array(  
  78.             'offset' => $this->offset,  
  79.             'from' => $from,  
  80.             'to' => $to,  
  81.             'size' => $size,  
  82.             'no' => $no,  
  83.             'max' => $max,  
  84.             'total' => $total,  
  85.         );  
  86.     }  
  87.  
  88.     public function GenWap() {  
  89.         $r = $this->paginate();  
  90.         $pagestring'<p align="right">';  
  91.         if( $this->pageNo > 1 ){  
  92.             $pageString.'4 <a href="http://www.cxybl.com/' . $this->genURL($this->pageString, $this->pageNo-1) . '" accesskey="4">上页</a>';  
  93.         }  
  94.         if( $this->pageNo >1 && $this->pageNo < $this->pageCount ){  
  95.             $pageString.'|';  
  96.         }  
  97.         if( $this->pageNo < $this->pageCount ) {  
  98.             $pageString.'<a href="http://www.cxybl.com/' .$this->genURL($this->pageString, $this->pageNo+1) . '" accesskey="6">下页</a> 6';  
  99.         }  
  100.         $pageString.'</p>';  
  101.         return $pageString;  
  102.     }  
  103.  
  104.     public function GenBasic() {  
  105.         $r = $this->paginate();  
  106.         $buffer = null;  
  107.         $index = '首页';  
  108.         $pre = '上一页';  
  109.         $next = '下一页';  
  110.         $last = '末页';  
  111.  
  112.         if ($this->pageCount<=7) {   
  113.             $rangerange = range(1,$this->pageCount);  
  114.         } else {  
  115.             $min = $this->pageNo - 3;  
  116.             $max = $this->pageNo + 3;  
  117.             if ($min < 1) {  
  118.                 $max += (3-$min);  
  119.                 $min = 1;  
  120.             }  
  121.             if ( $max > $this->pageCount ) {  
  122.                 $min -= ( $max - $this->pageCount );  
  123.                 $max = $this->pageCount;  
  124.             }  
  125.             $min = ($min>1) ? $min : 1;  
  126.             $rangerange = range($min, $max);  
  127.         }  
  128.           
  129.         $buffer .'<ul class="paginator">';  
  130.         $buffer ."<li>({$this->rowCount})</li>";  
  131.         if ($this->pageNo > 1) {  
  132.             $buffer ."<li><a href='http://www.cxybl.com/".$this->genURL($this->pageString,1)."'>{$index}</a><li><a href='http://www.cxybl.com/".$this->genURL($this->pageString,$this->pageNo-1)."'>{$pre}</a>";  
  133.         }  
  134.         foreach($range AS $one) {  
  135.             if ( $one == $this->pageNo ) {  
  136.                 $buffer .= "<li class=/"current/">{$one}</li>";  
  137.             } else {  
  138.                 $buffer ."<li><a href='http://www.cxybl.com/".$this->genURL($this->pageString,$one)."'>{$one}</a><li>";  
  139.             }  
  140.         }  
  141.         if ($this->pageNo < $this->pageCount) {  
  142.             $buffer ."<li><a href='http://www.cxybl.com/".$this->genURL($this->pageString,$this->pageNo+1)."'>{$next}</a></li><li><a href='http://www.cxybl.com/".$this->genURL($this->pageString, $this->pageCount)."'>{$last}</a></li>";  
  143.         }  
  144.         return $buffer . '</ul>';  
  145.     }  
  146. }  
  147. ?> 
TEUHTML5中文学习网 - HTML5先行者学习网
TEUHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助