之前寫的一個php分頁類,基本上能實現你們一些分頁功能的實現,具體關於怎麼使用你們能夠看看我空間的另外一篇關於,記錄ip和pv的博文,主要是在查詢數據庫時當咱們組織sql語句的時候,加上 limit 就能夠了,若是表單使用get方式也能夠實現搜索結果的分頁顯示,由於在類中已經組織好了URL地址。javascript
- <?php
- class Page{
- private $total_rows; //總記錄數
- private $total_page; //總頁數
- private $onepage_rows; //每頁顯示行數
- private $self_page; //當前頁
- private $url; //URL地址
- private $page_rows; //頁碼數量
- private $start_id; //當前頁起始ID
- private $end_id; //當前頁結束ID
- private $desc=array();
- /**
- * 構造函數
- *
- * @access public
- * @param integer $total 總條數
- * @param integer $rows 每頁顯示行數
- * @param integer $page_rows 頁碼數量
- * @param string $desc 分頁文字描述
- */
- function __construct($total,$rows=10,$page_rows=8,$desc=''){
- $this->total_rows = $total;//總條數
- $this->onepage_rows = $rows;//每頁顯示行數
- $this->page_rows = $page_rows;//頁碼數量
- $this->total_page = ceil($this->total_rows/$this->onepage_rows);//總頁數
- $this->self_page = min($this->total_page,max((int)@$_GET['page'],1));//當前頁
- $this->start_id = ($this->self_page-1)*$this->onepage_rows+1;//起始ID
- $this->end_id = min($this->total_rows,$this->self_page*$this->onepage_rows);
- $this->url = $this->requestUrl();//配置URL地址
- $this->desc = $this->desc($desc);//分頁文字描述
- //var_dump($this->desc);
- }
- /**
- * 配置url地址
- *
- * @access private
- * @return string 返回配置好的url地址
- */
- private function requestUrl(){
- /* echo "<pre>";
- //print_r($_SERVER);
- echo "</pre>";
- */
- $url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING'];
- //解析url地址,返回數組
- $request_arr = parse_url( $url );
- if( isset($request_arr['query']) ){
- //解析請求參數
- parse_str($request_arr['query'],$arr);
- //刪除參數中的page元素
- unset($arr['page']);
- //合併路徑及請求參數爲標準的URL地址
- $url = $request_arr['path'].'?'.http_build_query($arr).'&page=';
- }else{
- //沒有請求參數GET的狀況
- $url = strstr($url, '?') ? $url."page=" : $url."?page=";
- }
- return $url;
- }
- /**
- * 配置 分頁文字描述方法
- *
- * @access private
- * @param array $desc 分頁的描述
- * @return array
- */
- private function desc($desc){
- //默認文字描述
- $d = array(
- "pre"=>"上一頁",
- "next"=>"下一頁",
- "first"=>"首頁",
- "end"=>"末頁",
- "unit"=>"條",);
- if( emptyempty($desc) || !is_array($desc) ){
- return $d;
- }
- //過濾沒有值的數組
- function filter($v){
- return !emptyempty($v);
- }
- //合併數組
- return array_merge( $d , array_filter($desc,"filter") );
- }
- /**
- * SQL limit語句,控制每頁顯示行數
- *
- * @access public
- * @return string
- */
- function limit(){
- return "LIMIT ".max( 0 , ($this->self_page-1)*$this->onepage_rows ).",".$this->onepage_rows;
- }
- /**
- * 上一頁
- *
- * @access public
- * @return string
- */
- function pre(){
- return $this->self_page > 1 ? "<a href='".$this->url.($this->self_page-1)."'>".$this->desc['pre']."</a>" : "";
- }
- /**
- * 下一頁
- *
- * @access public
- * @return string
- */
- function next(){
- return $this->self_page < $this->total_page ? "<a href='".$this->url.($this->self_page+1)."'>".$this->desc['next']."</a>" : "";
- }
- /**
- * 首頁
- *
- * @access public
- * @return string
- */
- function first(){
- return $this->self_page > 1 ? "<a href='{$this->url}1'>{$this->desc['first']}</a>" : "";
- }
- /**
- * 尾頁
- *
- * @access public
- * @return string
- */
- function end(){
- return $this->self_page < $this->total_page ? "<a href='{$this->url}{$this->total_page}'>{$this->desc['end']}</a>" : "";
- }
- /**
- * 當前記錄數
- *
- * @access public
- * @return string
- */
- function nowPage(){
- return "第".$this->start_id."{$this->desc['unit']}-{$this->end_id}{$this->desc['unit']}";
- }
- /**
- * 返回當前頁碼
- *
- * @access public
- * @return string
- */
- function selfnum(){
- return $this->self_page;
- }
- /**
- * 統計數據信息
- *
- * @access public
- * @return string
- */
- function count(){
- return "<span>總共有: {$this->total_page} 頁 總計: {$this->total_rows} 條</span>";
- }
- /**
- * 前幾頁
- *
- * @access public
- * @return string
- */
- function pres(){
- $num = $this->self_page - $this->page_rows;
- return $this->self_page > $this->page_rows ? "<a href='".$this->url.$num."'>".'前'.$this->page_rows.'頁'."</a>" :"";
- }
- /**
- * 後幾頁
- *
- * @access public
- * @return string
- */
- function nests(){
- $num = $this->self_page + $this->page_rows;
- return $this->self_page < $this->total_page - $this->page_rows ? "<a href='{$this->url}$num'>後{$this->page_rows}頁</a>" : "";
- }
- /**
- * 得到頁面數組
- *
- * @access private
- * @return string
- */
- private function pagelist(){
- $pageList = array();
- $start = max( 1 , min( $this->self_page - ceil($this->page_rows/2), $this->total_page - $this->page_rows ));
- $end = min( $this->total_page , $start+$this->page_rows );
- for( $i=$start;$i<=$end;$i++ ){
- if( $i == $this->self_page ){
- //變量是當前頁時,去掉連接
- $pageList[$i]['url'] = "";
- $pageList[$i]['str'] = $i;
- continue;
- }
- $pageList[$i]['url'] = $this->url.$i;
- $pageList[$i]['str'] = $i;
- }
- return $pageList;
- }
- /**
- * 字符串分頁
- *
- * @access public
- * @return string
- */
- function strList(){
- $arr = $this->pagelist();
- $pageList = "";
- foreach( $arr as $v ){
- $pageList .= emptyempty($v['url']) ? "<strong>".$v['str']."</strong>" : "<a class='alink' href='{$v['url']}'>{$v['str']}</a>";
- }
- return $pageList;
- }
- /**
- * 下拉列表分頁
- *
- * @access public
- * @return string
- */
- function selectList(){
- $arr = $this->pagelist();
- $pageList = "";
- $pageList .= "<select class='sel' onchange='javascript:location.href = this.options[selectedIndex].value'>";
- foreach( $arr as $v ){
- $pageList .= emptyempty($v['url']) ? "<option value='{$v['url']}{$v['str']}' selected='selected'>{$v['str']}</option>" : "<option value='{$v['url']}'>{$v['str']}</option>";
- }
- $pageList .= "</select>";
- return $pageList;
- }
- /**
- * 直接輸入 跳轉分頁
- *
- * @access public
- * @return string
- */
- function inputList(){
- //相應 enter 鍵盤事件
- $pageList = "<input type='text' value = '{$this->self_page}' id='pageinput' class='pageinput' onkeydown=\"
- javascript:if(event.keyCode==13){
- location.href='{$this->url}'+this.value;
- };\"/>
- <button
- var url = document.getElementById('pageinput').value;
- location.href='{$this->url}'+url;
- \"/>
- 跳轉</button>
- ";
- return $pageList;
- }
- /**
- * 分頁幾種組合樣式
- *
- * @param integer $style_id 樣式的編號
- * @return string
- */
- public function styleList($style_id){
- switch ($style_id){
- case 1:
- return $this->pre().$this->strlist().$this->next();
- case 2:
- return $this->pre().$this->strlist().$this->next().$this->count();
- case 3:
- return $this->pres().$this->select().$this->nexts();
- }
- }
- }