超强分页类2.0发布,支持自定义风格,默认4种显示模式
看到论坛上有人求百度分页类,所以把几周前的分页类贴出来。
2.0相对1.0修正如下:
1、支持PHP4和PHP5
2、增加了对ajax分页的功能
3、优化了程序整体结构
4、增加了自定义风格的功能
代码如下:
<? <BR>/** <BR> * filename: ext_page.class.php <BR> * @package:phpbean <BR> * @author :feifengxlq <BR> * @copyright :Copyright 2006 feifengxlq <BR> * @license:version 2.0 <BR> * @create:2006-5-31 <BR> * @modify:2006-6-1 <BR> * @modify:feifengxlq 2006-11-4 <BR> * description:超强分页类,四种分页模式,默认采用类似baidu,google的分页风格。 <BR> * 2.0增加功能:支持自定义风格,自定义样式,同时支持PHP4和PHP5, <BR> * to see detail,please visit [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=new page(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=new page(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>class page <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> /** <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> /** <BR> * constructor构造函数 <BR> * <BR> * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']... <BR> */ <BR> function page($array) <BR> { <BR> if(is_array($array)){ <BR> if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total'); <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))||($totalerror(__FUNCTION__,$total.' is not a positive integer!'); <BR> if((!is_int($perpage))||($perpageerror(__FUNCTION__,$perpage.' is not a positive integer!'); <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> * @param string $var <BR> * @param string $value <BR> */ <BR> function set($var,$value) <BR> { <BR> if(in_array($var,get_object_vars($this))) <BR> $this->$var=$value; <BR> else { <BR> $this->error(__FUNCTION__,$var." does not belong to PB_Page!"); <BR> } <br><br> } <BR> /** <BR> * 打开倒AJAX模式 <BR> * <BR> * @param string $action 默认ajax触发的动作。 <BR> */ <BR> function open_ajax($action) <BR> { <BR> $this->is_ajax=true; <BR> $this->ajax_action_name=$action; <BR> } <BR> /** <BR> * 获取显示"下一页"的代码 <BR> * <BR> * @param string $style <BR> * @return string <BR> */ <BR> function next_page($style='') <BR> { <BR> if($this->nowindextotalpage){ <BR> return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style); <BR> } <BR> return '<span class="'.$style.'">'.$this->next_page.'</span>'; <BR> } <br><br> /** <BR> * 获取显示“上一页”的代码 <BR> * <BR> * @param string $style <BR> * @return string <BR> */ <BR> function pre_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 '<span class="'.$style.'">'.$this->pre_page.'</span>'; <BR> } <br><br> /** <BR> * 获取显示“首页”的代码 <BR> * <BR> * @return string <BR> */ <BR> function first_page($style='') <BR> { <BR> if($this->nowindex==1){ <BR> return '<span class="'.$style.'">'.$this->first_page.'</span>'; <BR> } <BR> return $this->_get_link($this->_get_url(1),$this->first_page,$style); <BR> } <br><br> /** <BR> * 获取显示“尾页”的代码 <BR> * <BR> * @return string <BR> */ <BR> function last_page($style='') <BR> { <BR> if($this->nowindex==$this->totalpage){ <BR> return '<span class="'.$style.'">'.$this->last_page.'</span>'; <BR> } <BR> return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style); <BR> } <br><br> function nowbar($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;$ipagebarnum;$i++) <BR> { <BR> if($itotalpage){ <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('<span class="'.$nowindex_style.'">'.$i.'</span>'); <BR> }else{ <BR> break; <BR> } <BR> $return.="\n"; <BR> } <BR> unset($begin); <BR> return $return; <BR> } <BR> /** <BR> * 获取显示跳转按钮的代码 <BR> * <BR> * @return string <BR> */ <BR> function select() <BR> { <BR> $return=''; <BR> for($i=1;$itotalpage;$i++) <BR> { <BR> if($i==$this->nowindex){ <BR> $return.=''.$i.''; <BR> }else{ <BR> $return.=''.$i.''; <BR> } <BR> } <BR> unset($i); <BR> $return.=''; <BR> return $return; <BR> } <br><br> /** <BR> * 获取mysql 语句中limit需要的值 <BR> * <BR> * @return string <BR> */ <BR> function offset() <BR> { <BR> return $this->offset; <BR> } <br><br> /** <BR> * 控制分页显示风格(你可以增加相应的风格) <BR> * <BR> * @param int $mode <BR> * @return string <BR> */ <BR> function show($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>/*----------------private function (私有方法)-----------------------------------------------------------*/ <BR> /** <BR> * 设置url头地址 <BR> * @param: String $url <BR> * @return boolean <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> }//end if <BR> }//end if <BR> }//end if <BR> } <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> * @param int $pageno <BR> * @return string $url <BR> */ <BR> function _get_url($pageno=1) <BR> { <BR> return <strong>)本文来(源gaodai#ma#com搞@@代~&码*网2</strong><pre>搞代gaodaima码
$this->url.$pageno;
}
/**
* 获取分页显示文字,比如说默认情况下_get_text(‘1’)将返回[1]
*
* @param String $str
* @return string $url
*/
function _get_text($str)
{
return $this->format_left.$str.$this->format_right;
}
/**
* 获取链接地址
*/
function _get_link($url,$text,$style=”){
$style=(empty($style))?”:’class=”‘.$style.'”‘;
if($this->is_ajax){
//如果是使用AJAX模式
return ‘ajax_action_name.'(\”.$url.’\’)”>’.$text.”;
}else{
return ”.$text.”;
}
}
/**
* 出错处理方式
*/
function error($function,$errormsg)
{
die(‘Error in file ‘.__FILE__.’ ,Function ‘.$function.'() :’.$errormsg);
}
}
?>
提供一个简单的演示demo
<? <BR>require_once('../libs/classes/page.class.php'); <BR>$page=new page(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=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test')); <BR>echo 'mode:1<br>'.$ajaxpage->show(); <BR>?>
演示地址:http://traffic02.100steps.net/52site/test/page.php