php 经典的分页代码类
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-09 12:50:22 浏览: 评论:0
这是一款php分页代码是一款经典的可自动判断url参数再进行分页的实例代码,同时自定了上页,下页,首页,尾页,同时还支持select下拉框跳转代码.
php 经典的分页代码类代码如下:
- class page{
- var $page_name="page";
- var $next_page='下一页';
- var $pre_page='上一页';
- var $first_page='首页';
- var $last_page='尾页';
- var $pre_bar='<<';
- var $next_bar='>>';
- var $format_left='';
- var $format_right='';
- var $pagebarnum=5;
- var $totalpage=0;
- var $nowindex=1;
- var $url="";
- var $offset=0;
- var $rewrite = array();
- function page($array)
- {
- if(is_array($array)){
- if(!array_key_exists('total',$array))$this->error(__function__,'need a param of total');
- $total=intval($array['total']);
- $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
- $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
- $url=(array_key_exists('url',$array))?$array['url']:'';
- $action = (array_key_exists('action', $array)) ? $array['action'] : '';
- $id0 = (array_key_exists('id0', $array)) ? $array['id0'] : '';
- $id1 = (array_key_exists('id1', $array)) ? $array['id1'] : '';
- $id2 = (array_key_exists('id2', $array)) ? $array['id2'] : '';
- $id3 = (array_key_exists('id3', $array)) ? $array['id3'] : '';
- }else{
- $total=$array;
- $perpage=10;
- $nowindex='';
- $url='';
- $action = '';
- $id0 = '';
- $id1 = '';
- $id2 = '';
- $id3 = '';
- }
- if((!is_int($total))||($total<0))$this->error(__function__,$total.' is not a positive integer!');
- if((!is_int($perpage))||($perpage<=0))$this->error(__function__,$perpage.' is not a positive integer!');
- if(!emptyempty($array['page_name']))$this->set('page_name',$array['page_name']);
- $this->_set_nowindex($nowindex);
- $this->_set_url($url);
- $this->totalpage=ceil($total/$perpage);
- $this->offset=($this->nowindex-1)*$perpage;
- $this->action = $action;
- $this->rewrite = array('action'=>$action,'id0'=>$id0, 'id1'=>$id1, 'id2'=>$id2, 'id3'=>$id3);
- }
- function set($var,$value)
- {
- if(in_array($var,get_object_vars($this)))
- $this->$var=$value;
- else {
- $this->error(__function__,$var." does not belong to pb_page!");
- }
- }
- function next_page($style=''){
- if($this->nowindex<$this->totalpage){
- return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
- }
- return '<a class="'.$style.'">'.$this->next_page.'</a>';
- }
- function pre_page($style=''){
- if($this->nowindex>1){
- return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
- }
- return '<a class="'.$style.'">'.$this->pre_page.'</a>';
- }
- function first_page($style=''){
- if($this->nowindex==1){
- return '<a class="'.$style.'">'.$this->first_page.'</a>';
- }
- return $this->_get_link($this->_get_url(1),$this->first_page,$style);
- }
- function last_page($style=''){
- if($this->nowindex==$this->totalpage||$this->totalpage==0){
- return '<a class="'.$style.'">'.$this->last_page.'</a>';
- }
- return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
- }
- function nowbar($style='',$nowindex_style='')
- {
- $plus=ceil($this->pagebarnum/2);
- if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
- $begin=$this->nowindex-$plus+1;
- $begin=($begin>=1)?$begin:1;
- $return='';
- for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
- {
- if($i<=$this->totalpage){
- if($i!=$this->nowindex)
- $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
- else
- $return.=$this->_get_text('<a class="'.$nowindex_style.'">'.$i.'</a>');
- }else{
- break;
- }
- $return.=" ";
- }
- unset($begin);
- return $return;
- }
- /**
- * 获取显示跳转按钮的代码
- *
- * @return string
- */
- function select()
- {
- $return='<select name="pb_page_select">';
- for($i=1;$i<=$this->totalpage;$i++)
- {
- if($i==$this->nowindex){
- $return.='<option value="'.$i.'" selected>'.$i.'</option>';
- }else{
- $return.='<option value="'.$i.'">'.$i.'</option>';
- }
- }
- unset($i);
- $return.='</select>';
- return $return;
- }
- /**
- * 获取mysql教程 语句中limit需要的值
- *
- * @return string
- */
- function offset()
- {
- return $this->offset;
- }
- /**
- * 控制分页显示风格(你可以增加相应的风格)
- *
- * @param int $mode
- * @return string
- */
- function show($mode=1)
- {
- switch ($mode)
- {
- case '1':
- $this->next_page='下一页';
- $this->pre_page='上一页';
- return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
- break;
- case '2':
- $this->next_page='下一页';
- $this->pre_page='上一页';
- $this->first_page='首页';
- $this->last_page='尾页';
- return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
- break;
- case '3':
- $this->next_page='下一页';
- $this->pre_page='上一页';
- $this->first_page='首页';
- $this->last_page='尾页';
- return $this->first_page("page_box")."".$this->pre_page("page_box")."".$this->nowbar("page_box_a","page_box_b")."".$this->next_page("page_box")."".$this->last_page("page_box")."<a class="clear"></a>";
- break;
- case '4':
- $this->next_page='下一页';
- $this->pre_page='上一页';
- return $this->pre_page().$this->nowbar().$this->next_page();
- break;
- case '5':
- return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
- break;
- }
- }
- function _set_url($url="")
- {
- if(!emptyempty($url)){
- $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
- }else{
- if(emptyempty($_server['query_string'])){
- $this->url=$_server['request_uri']."?".$this->page_name."=";
- }else{
- if(stristr($_server['query_string'],$this->page_name.'=')){
- $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_server['request_uri']);
- $last=$this->url[strlen($this->url)-1];
- if($last=='?'||$last=='&'){
- $this->url.=$this->page_name."=";
- }else{
- $this->url.='&'.$this->page_name."=";
- }
- }else{
- $this->url=$_server['request_uri'].'&'.$this->page_name.'=';
- }
- }
- }
- }
- function _set_nowindex($nowindex)
- {
- if(emptyempty($nowindex)){
- if(isset($_get[$this->page_name])){
- $this->nowindex=intval($_get[$this->page_name]);
- }
- }else{
- $this->nowindex=intval($nowindex);
- }
- }
- function _get_url($pageno=1)
- {
- global $_cfg;
- $arr = $this->rewrite;
- //print_r($arr);
- //print_r($this->url.$pageno);
- if($_cfg['urlrewrite'] && !emptyempty($arr['action'])){
- return url_rewrite($arr['action'], array('id0'=>$arr['id0'],'id1'=>$arr['id1'],'id2'=>$arr['id2'],'id3'=>$arr['id3'],'page'=>$pageno));
- }else{
- return $this->url.$pageno;
- }
- }
- function _get_text($str)
- {
- return $this->format_left.$str.$this->format_right;
- }
- function _get_link($url,$text,$style=''){
- $style=(emptyempty($style))?'':'class="'.$style.'"';
- return '<a '.$style.' href="'.$url.'">'.$text.'</a>';
- }
- function error($function,$errormsg)
- //开源代码phpfensi.com
- {
- die('error in file <b>'.__file__.'</b> ,function <b>'.$function.'()</b> :'.$errormsg);
- }
- }
Tags: php经典分页 php分页
- 上一篇:php 文章内容分页代码
- 下一篇:ajax +php无刷新分页代码
相关文章
- ·php mysql经典分页程序(2014-09-11)
- ·php分页代码与分页原理(2013-11-12)
- ·PHP分页显示制作(2013-11-13)
- ·PHP实现翻页跳转功能(2013-11-13)
- ·自定义PHP分页函数(2013-11-13)
- ·小谈PHP&MYSQL分页原理及实现(2013-11-13)
- ·php中分页显示文章标题(2013-12-10)
- ·PHP 分页代码与分页原理解析(2014-01-06)
- ·php文章内容分页实例程序(2014-01-07)
- ·php分页可利用表格来分页类(2014-01-15)
- ·简单入门级php分页代码(2014-05-10)
- ·php分页实例程序函数(2014-07-18)
- ·二个php分页程序代码(2014-07-29)
- ·php分页多种实现程序代码(2014-07-31)
- ·两款实用php分页代码(2014-08-15)
- ·经典php分页代码,支持数字偏移(2014-08-15)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)