«
超强分页类2.0发布,支持自定义风格,默认4种显示模式

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  看到论坛上有人求百度分页类,所以把几周前的分页类贴出来。</p><p>  2.0相对1.0修正如下:</p><p>  1、支持PHP4和PHP5</p><p>  2、增加了对ajax分页的功能</p><p>  3、优化了程序整体结构</p><p>  4、增加了自定义风格的功能</p><p>  代码如下:</p><p>  PHP代码:</p><code><?<br />/**<br />*filename:ext_page.class.php<br />*@package:phpbean<br />*@author:feifengxlq<feifengxlq#gmail.com><[url=http://www.phpobject.net/]http://www.phpobject.net/[/url]><br />*@copyright:Copyright2006feifengxlq<br />*@license:version2.0<br />*@create:2006-5-31<br />*@modify:2006-6-1<br />*@modify:feifengxlq2006-11-4<br />*description:超强分页类,四种分页模式,默认采用类似baidu,google的分页风格。<br />*2.0增加功能:支持自定义风格,自定义样式,同时支持PHP4和PHP5,<br />*toseedetail,pleasevisit[url=http://www.phpobject.net/blog/read.php]http://www.phpobject.net/blog/read.php[/url]?<br />*example:<br />*模式四种分页模式:<br />  require_once('../libs/classes/page.class.php');<br />  $page=newpage(array('total'=>1000,'perpage'=>20));<br />  echo'mode:1<br>'.$page->show();<br />  echo'<hr>mode:2<br>'.$page->show(2);<br />  echo'<hr>mode:3<br>'.$page->show(3);<br />  echo'<hr>mode:4<br>'.$page->show(4);<br />  开启AJAX:<br />  $ajaxpage=newpage(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));<br />  echo'mode:1<br>'.$ajaxpage->show();<br />  采用继承自定义分页显示模式:<br />  demo:http://www.phpobject.net/blog<br />*/<br />classpage<br />{<br />/**<br />  *config,public<br />  */<br />var$page_name="PB_page";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page<br />var$next_page='>';//下一页<br />var$pre_page='<';//上一页<br />var$first_page='First';//首页<br />var$last_page='Last';//尾页<br />var$pre_bar='<<';//上一分页条<br />var$next_bar='>>';//下一分页条<br />var$format_left='[';<br />var$format_right=']';<br />var$is_ajax=false;//是否支持AJAX分页模式<br />/**<br />  *private<br />  *<br />  */<br />var$pagebarnum=10;//控制记录条的个数。<br />var$totalpage=0;//总页数<br />var$ajax_action_name='';//AJAX动作名<br />var$nowindex=1;//当前页<br />var$url="";//url地址头<br />var$offset=0;<br />/**<br />  *constructor构造函数<br />  *<br />  *@paramarray$array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...<br />  */<br />functionpage($array)<br />{<br />  if(is_array($array)){<br />   if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'needaparamoftotal');<br />   $total=intval($array['total']);<br />   $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;<br />   $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';<br />   $url=(array_key_exists('url',$array))?$array['url']:'';<br />  }else{<br />   $total=$array;<br />   $perpage=10;<br />   $nowindex='';<br />   $url='';<br />  }<br />  if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.'isnotapositiveinteger!');<br />  if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.'isnotapositiveinteger!');<br />  if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename<br />  $this->_set_nowindex($nowindex);//设置当前页<br />  $this->_set_url($url);//设置链接地址<br />  $this->totalpage=ceil($total/$perpage);<br />  $this->offset=($this->nowindex-1)*$this->perpage;<br />  if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式<br />}<br />/**<br />  *设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception<br />  *<br />  *@paramstring$var<br />  *@paramstring$value<br />  */<br />functionset($var,$value)<br />{<br />  if(in_array($var,get_object_vars($this)))<br />   $this->$var=$value;<br />  else{<br />  $this->error(__FUNCTION__,$var."doesnotbelongtoPB_Page!");<br />  }<br /> <br />}<br />/**<br />  *打开倒AJAX模式<br />  *<br />  *@paramstring$action默认ajax触发的动作。<br />  */<br />functionopen_ajax($action)<br />{<br />  $this->is_ajax=true;<br />  $this->ajax_action_name=$action;<br />}<br />/**<br />  *获取显示"下一页"的代码<br />  *<br />  *@paramstring$style<br />  *@returnstring<br />  */<br />functionnext_page($style='')<br />{<br />  if($this->nowindex<$this->totalpage){<br />  return$this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);<br />  }<br />  return'<spanclass="'.$style.'">'.$this->next_page.'</span>';<br />}<br />/**<br />  *获取显示“上一页”的代码<br />  *<br />  *@paramstring$style<br />  *@returnstring<br />  */<br />functionpre_page($style='')<br />{<br />  if($this->nowindex>1){<br />  return$this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);<br />  }<br />  return'<spanclass="'.$style.'">'.$this->pre_page.'</span>';<br />}<br />/**<br />  *获取显示“首页”的代码<br />  *<br />  *@returnstring<br />  */<br />functionfirst_page($style='')<br />{<br />  if($this->nowindex==1){<br />    return'<spanclass="'.$style.'">'.$this->first_page.'</span>';<br />  }<br />  return$this->_get_link($this->_get_url(1),$this->first_page,$style);<br />}<br />/**<br />  *获取显示“尾页”的代码<br />  *<br />  *@returnstring<br />  */<br />functionlast_page($style='')<br />{<br />  if($this->nowindex==$this->totalpage){<br />    return'<spanclass="'.$style.'">'.$this->last_page.'</span>';<br />  }<br />  return$this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);<br />}<br />functionnowbar($style='',$nowindex_style='')<br />{<br />  $plus=ceil($this->pagebarnum/2);<br />  if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);<br />  $begin=$this->nowindex-$plus+1;<br />  $begin=($begin>=1)?$begin:1;<br />  $return='';<br />  for($i=$begin;$i<$begin+$this->pagebarnum;$i++)<br />  {<br />  if($i<=$this->totalpage){<br />   if($i!=$this->nowindex)<br />     $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));<br />   else<br />     $return.=$this->_get_text('<spanclass="'.$nowindex_style.'">'.$i.'</span>');<br />  }else{<br />   break;<br />  }<br />  $return.=" ";<br />  }<br />  unset($begin);<br />  return$return;<br />}<br />/**<br />  *获取显示跳转按钮的代码<br />  *<br />  *@returnstring<br />  */<br />functionselect()<br />{<br />  $return='<selectname="PB_Page_Select">';<br />  for($i=1;$i<=$this->totalpage;$i++)<br />  {<br />  if($i==$this->nowindex){<br />   $return.='<optionvalue="'.$i.'"selected>'.$i.'</option>';<br />  }else{<br />   $return.='<optionvalue="'.$i.'">'.$i.'</option>';<br />  }<br />  }<br />  unset($i);<br />  $return.='</select>';<br />  return$return;<br />}<br />/**<br />  *获取mysql语句中limit需要的值<br />  *<br />  *@returnstring<br />  */<br />functionoffset()<br />{<br />  return$this->offset;<br />}<br />/**<br />  *控制分页显示风格(你可以增加相应的风格)<br />  *<br />  *@paramint$mode<br />  *@returnstring<br />  */<br />functionshow($mode=1)<br />{<br />  switch($mode)<br />  {<br />  case'1':<br />   $this->next_page='下一页';<br />   $this->pre_page='上一页';<br />   return$this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';<br />   break;<br />  case'2':<br />   $this->next_page='下一页';<br />   $this->pre_page='上一页';<br />   $this->first_page='首页';<br />   $this->last_page='尾页';<br />   return$this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';<br />   break;<br />  case'3':<br />   $this->next_page='下一页';<br />   $this->pre_page='上一页';<br />   $this->first_page='首页';<br />   $this->last_page='尾页';<br />   return$this->first_page().$this->pre_page().$this->next_page().$this->last_page();<br />   break;<br />  case'4':<br />   $this->next_page='下一页';<br />   $this->pre_page='上一页';<br />   return$this->pre_page().$this->nowbar().$this->next_page();<br />   break;<br />  case'5':<br />   return$this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();<br />   break;<br />  }<br /> <br />}<br />/*----------------privatefunction(私有方法)-----------------------------------------------------------*/<br />/**<br />  *设置url头地址<br />  *@param:String$url<br />  *@returnboolean<br />  */<br />function_set_url($url="")<br />{<br />  if(!empty($url)){<br />    //手动设置<br />  $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";<br />  }else{<br />    //自动获取<br />  if(empty($_SERVER['QUERY_STRING'])){<br />    //不存在QUERY_STRING时<br />   $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";<br />  }else{<br />    //<br />   if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){<br />     //地址存在页面参数<br />   $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);<br />   $last=$this->url[strlen($this->url)-1];<br />   if($last=='?'||$last=='&'){<br />     $this->url.=$this->page_name."=";<br />   }else{<br />     $this->url.='&'.$this->page_name."=";<br />   }<br />   }else{<br />     //<br />   $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';<br />   }//endif  <br />  }//endif<br />  }//endif<br />}<br />/**<br />  *设置当前页面<br />  *<br />  */<br />function_set_nowindex($nowindex)<br />{<br />  if(empty($nowindex)){<br />  //系统获取<br /> <br />  if(isset($_GET[$this->page_name])){<br />   $this->nowindex=intval($_GET[$this->page_name]);<br />  }<br />  }else{<br />    //手动设置<br />  $this->nowindex=intval($nowindex);<br />  }<br />}<br /> <br />/**<br />  *为指定的页面返回地址值<br />  *<br />  *@paramint$pageno<br />  *@returnstring$url<br />  */<br />function_get_url($pageno=1)<br />{<br />  return$this->url.$pageno;<br />}<br />/**<br />  *获取分页显示文字,比如说默认情况下_get_text('<ahref="">1</a>')将返回[<ahref="">1</a>]<br />  *<br />  *@paramString$str<br />  *@returnstring$url<br />  */<br />function_get_text($str)<br />{<br />  return$this->format_left.$str.$this->format_right;<br />}<br />/**<br />  *获取链接地址<br />*/<br />function_get_link($url,$text,$style=''){<br />  $style=(empty($style))?'':'class="'.$style.'"';<br />  if($this->is_ajax){<br />    //如果是使用AJAX模式<br />  return'<a'.$style.'href="javascript:'.$this->ajax_action_name.'(''.$url.'')">'.$text.'</a>';<br />  }else{<br />  return'<a'.$style.'href="'.$url.'">'.$text.'</a>';<br />  }<br />}<br />/**<br />  *出错处理方式<br />*/<br />functionerror($function,$errormsg)<br />{<br />   die('Errorinfile<b>'.__FILE__.'</b>,Function<b>'.$function.'()</b>:'.$errormsg);<br />}<br />}<br />?></code><p>  提供一个简单的演示demo</p>
<p> </p>

   <p>  PHP代码:</p><code><?<br />require_once('../libs/classes/page.class.php');<br />$page=newpage(array('total'=>1000,'perpage'=>20));<br />echo'mode:1<br>'.$page->show();<br />echo'<hr>mode:2<br>'.$page->show(2);<br />echo'<hr>mode:3<br>'.$page->show(3);<br />echo'<hr>mode:4<br>'.$page->show(4);<br />echo'<hr>开始AJAX模式:';<br />$ajaxpage=newpage(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));<br />echo'mode:1<br>'.$ajaxpage->show();<br />?></code><p>  效果图</p><p>  <img src="/content/uploadfile/200805/2008053117354433.jpg" onclick="get_larger(this)" /></p></p>