Home > Backend Development > PHP Tutorial > PHP分页类实现

PHP分页类实现

WBOY
Release: 2016-06-20 13:05:13
Original
972 people have browsed it

分享一个常用的php分页类。有三种表现形式,具体效果图如下:


该php分页类的具体代码以及使用方法如下:

<p><?php</p><p>/**</p>*PHP分页类<br /> *<br />*show(2) 1 ... 62 63 64 65 66 67 68 ... 150<br />*分页样式<br />*#page{font:12px/16px arial}<br />*#page span{float:left;margin:0px 3px;}<br />*#page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px<br />*7px;text-decoration:none;color:#666}<br />*#page a.now_page,#page a:hover{color:#fff;background:#05c}<br /> */<br />class Pager{<br />public $first_row;//起始行数<br />public $list_rows;//列表每页显示行数<br />protected $total_pages;//总页数<br />protected $total_rows;//总行数<br />protected $now_page;//当前页数<br />protected $method='defalut';//处理情况 Ajax分页 Html分页(静态化时) 普通get方式<br />protected $parameter='';<br />protected $page_name;//分页参数的名称<br />protected $ajax_func_name;<br />public $plus=3;//分页偏移量<br />protected $url;<br />/**<br />*构造函数<br /> *<br />*@param unknown_type $data<br /> */<br />public function __construct($data=array()){<br />$this->total_rows=$data['total_rows'];<br />$this->parameter=!empty($data['parameter'])?$data['parameter']:'';<br />$this->list_rows=!empty($data['list_rows'])&&$data['list_rows']<=100?$data['list_rows']:15;<br />$this->total_pages=ceil($this->total_rows / $this->list_rows);<br />$this->page_name=!empty($data['page_name'])?$data['page_name']:'page';<br />$this->ajax_func_name=!empty($data['ajax_func_name'])?$data['ajax_func_name']:'';<br />$this->method=!empty($data['method'])?$data['method']:'';<br />/* 当前页面 */<br />if(!empty($data['now_page'])){<br />$this->now_page=intval($data['now_page']);<br />}else{<br />$this->now_page=!empty($_GET[$this->page_name])?intval($_GET[$this->page_name]):1;<br />}<br />$this->now_page=$this->now_page<=0?1:$this->now_page;<br />if(!empty($this->total_pages)&&$this->now_page>$this->total_pages){<br />$this->now_page=$this->total_pages;<br />}<br />$this->first_row=$this->list_rows*($this->now_page-1);<br />}<br />/**<br />*得到当前连接<br /> *<br />*@param<br />*       	$page<br />*@param<br />*       	$text<br />*@return string<br /> */<br />protected function _get_link($page,$text){<br />switch ($this->method){<br />case 'ajax' :<br />$parameter='';<br />if($this->parameter){<br />$parameter=','.$this->parameter;<br />}<br />return '<a onclick="'.$this->ajax_func_name.'('.$page.$parameter.')" href="javascript:void(0)">'.$text.'</a>'."";<br />break;<br />case 'html' :<br />$url=str_replace('?',$page,$this->parameter);<br />return '<a href="'.$url.'">'.$text.'</a>';<br />break;<br /><br />default :<br />return '<a href="'.$this->_get_url($page).'">'.$text.'</a>';<br />break;<br />}<br />}<br />/**<br />*设置当前页面链接<br /> */<br />protected function _set_url(){<br />$url=$_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;<br />$parse=parse_url($url);<br />if(isset($parse['query'])){<br />parse_str($parse['query'],$params);<br />unset($params[$this->page_name]);<br />$url=$parse['path'].'?'.http_build_query($params);<br />}<br />if(!empty($params)){<br />$url.='&';<br />}<br />$this->url=$url;<br />}<br />/**<br />*得到$page的url<br /> *<br />*@param $page 页面<br />*@return string<br /> */<br />protected function _get_url($page){<br />if($this->url === NULL){<br />$this->_set_url();<br />}<br />//	$lable=strpos('&',$this->url) === FALSE?'':'&';<br />return $this->url.$this->page_name.'='.$page;<br />}<br />/**<br />*得到第一页<br /> *<br />*@return string<br /> */<br />public function first_page($name='第一页'){<br />if($this->now_page>5){<br />return $this->_get_link('1',$name);<br />}<br />return '';<br />}<br />/**<br />*最后一页<br /> *<br />*@param<br />*       	$name<br />*@return string<br /> */<br />public function last_page($name='最后一页'){<br />if($this->now_page<$this->total_pages-5){<br />return $this->_get_link($this->total_pages,$name);<br />}<br />return '';<br />}<br />/**<br />*上一页<br /> *<br />*@return string<br /> */<br />public function up_page($name='上一页'){<br />if($this->now_page!=1){<br />return $this->_get_link($this->now_page-1,$name);<br />}<br />return '';<br />}<br />/**<br />*下一页<br /> *<br />*@return string<br /> */<br />public function down_page($name='下一页'){<br />if($this->now_page<$this->total_pages){<br />return $this->_get_link($this->now_page+1,$name);<br />}<br />return '';<br />}<br />/**<br />*分页样式输出<br /> *<br />*@param<br />*       	$param<br />*@return string<br /> */<br />public function show($param=1){<br />if($this->total_rows<1){<br />return '';<br />}<br />$className='show_'.$param;<br />$classNames=get_class_methods($this);<br />if(in_array($className,$classNames)){<br />return $this->$className();<br />}<br />return '';<br />}<br />protected function show_2(){<br />if($this->total_pages!=1){<br />$return='';<br />$return.=$this->up_page('<');<br />for($i=1;$i<=$this->total_pages;$i++){<br />if($i==$this->now_page){<br />$return.="<a class='now_page'>$i</a>";<br />}else{<br />if($this->now_page-$i>=4&&$i!=1){<br />$return.="<span class='pageMore'>...</span>";<br />$i=$this->now_page-3;<br />}else{<br />if($i>=$this->now_page+5&&$i!=$this->total_pages){<br />$return.="<span>...</span>";<br />$i=$this->total_pages;<br />}<br />$return.=$this->_get_link($i,$i);<br />}<br />}<br />}<br />$return.=$this->down_page('>');<br />return $return;<br />}<br />}<br />protected function show_1(){<br />$plus=$this->plus;<br />if($plus+$this->now_page>$this->total_pages){<br />$begin=$this->total_pages-$plus*2;<br />}else{<br />$begin=$this->now_page-$plus;<br />}<br /><br />$begin=($begin>=1)?$begin:1;<br />$return='';<br />$return.=$this->first_page();<br />$return.=$this->up_page();<br />for($i=$begin;$i<=$begin+$plus*2;$i++){<br />if($i>$this->total_pages){<br />break;<br />}<br />if($i==$this->now_page){<br />$return.="<a class='now_page'>$i</a>";<br />}else{<br />$return.=$this->_get_link($i,$i);<br />}<br />}<br />$return.=$this->down_page();<br />$return.=$this->last_page();<br />return $return;<br />}<br />protected function show_3(){<br />$plus=$this->plus;<br />if($plus+$this->now_page>$this->total_pages){<br />$begin=$this->total_pages-$plus*2;<br />}else{<br />$begin=$this->now_page-$plus;<br />}<br />$begin=($begin>=1)?$begin:1;<br />$return='总计 '.$this->total_rows.' 个记录分为 '.$this->total_pages.' 页,当前第 '.$this->now_page.' 页 ';<br />$return.=',每页 ';<br />$return.='<input type="text" value="'.$this->list_rows.'" id="pageSize" size="3"> ';<br />$return.=$this->first_page();<br />$return.=$this->up_page();<br />$return.=$this->down_page();<br />$return.=$this->last_page();<br />$return.='<select onchange="'.$this->ajax_func_name.'(this.value)" id="gotoPage">';<br />for($i=$begin;$i<=$begin+10;$i++){<br />if($i>$this->total_pages){<br />break;<br />}<br />if($i==$this->now_page){<br />$return.='<option selected="true" value="'.$i.'">'.$i.'</option>';<br />}else{<br />$return.='<option value="'.$i.'">'.$i.'</option>';<br />}<br />}<br />$return.='</select>';<br />return $return;<br />}<br /><p>}
Copy after login

类使用示例:###处理html静态化页面分页的情况###

# method 处理环境 设置为 html
# parameter 为静态页面参数 www.scutephp.com/20-0-0-0-40-?.html 注意问号
# ?问号的位置会自动替换为去向页码
# now_page 当前页面(静态页面获取不到当前页面所以只有你传入)
$params=array(<br />'total_rows'=>100,#(必须)<br />'method'    =>'html',#(必须)<br />'parameter' =>'www.scutephp.com/20-0-0-0-40-?.html', #(必须)<br />'now_page'  =>$_GET['p'], #(必须)<br />'list_rows' =>10,#(可选) 默认为15<br />);<br />$page=new Pager($params);<br />   echo $page->show(1);
Copy after login

#2

###处理ajax分页的情况###
# method 处理环境 设置为 ajax
# ajax_func_name ajax分页跳转页面的javascript方法
# parameter ajax_func_name后面的附带参数 默认为空
# now_page 不到当前页面所以只有你传入
$params=array(<br />'total_rows'=>100,<br />'method'    =>'ajax',<br />'ajax_func_name' =>'goToPage',<br />'now_page'  =>1,<br />#'parameter' =>"'jiong','username'",<br />);<br />$page=new Pager($params);<br />echo $page->show(1);
Copy after login

#7
#添加了parameter6


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template