当前位置:编程学习 > 网站相关 >>

让人崩溃的 AJAX模式下的分页


<?php
002 /**
003 演示
004 require_once('../libs/classes/page.class.php');
005 $page=new page(array('total'=>1000,'perpage'=>20));
006 echo 'mode:1<br>'.$page->show();
007 echo '<hr>mode:2<br>'.$page->show(2);
008 echo '<hr>mode:3<br>'.$page->show(3);
009 echo '<hr>mode:4<br>'.$page->show(4);
010 echo '<hr>开始AJAX模式:';
011 $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
012 echo 'mode:1<br>'.$ajaxpage->show();
013 */
014 class minupage
015 {
016 /**
017 * config ,public
018 */
019 var $page_name="p";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page
020 var $next_page='>';//下一页
021 var $pre_page='<';//上一页
022 var $first_page='First';//首页
023 var $last_page='Last';//尾页
024 var $pre_bar='<<';//上一分页条
025 var $next_bar='>>';//下一分页条
026 var $format_left='[';
027 var $format_right=']';
028 var $is_ajax=false;//是否支持AJAX分页模式
029 
030 /**
031 * private
032 *
033 */
034 var $pagebarnum=10;//控制记录条的个数。
035 var $totalpage=0;//总页数
036 var $ajax_action_name='';//AJAX动作名
037 var $nowindex=1;//当前页
038 var $url="";//url地址头
039 var $offset=0;
040 
041 /**
042 * constructor构造函数
043 *
044 * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...
045 */
046 function minupage($array)
047 {
048 if(is_array($array)){
049      if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total');
050      $total=intval($array['total']);
051      $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
052      $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
053      $url=(array_key_exists('url',$array))?$array['url']:'';
054 }else{
055      $total=$array;
056      $perpage=10;
057      $nowindex='';
058      $url='';
059 }
060 if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
061 if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
062 if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename
063 $this->_set_nowindex($nowindex);//设置当前页
064 $this->_set_url($url);//设置链接地址
065 $this->totalpage=ceil($total/$perpage);
066 $this->offset=($this->nowindex-1)*$perpage;
067 if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
068 }
069 /**
070 * 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception
071 *
072 * @param string $var
073 * @param string $value
074 */
075 function set($var,$value)
076 {
077 if(in_array($var,get_object_vars($this)))
078      $this->$var=$value;
079 else {
080    $this->error(__FUNCTION__,$var." does not belong to PB_Page!");
081 }
082 
083 }
084 /**
085 * 打开倒AJAX模式
086 *
087 * @param string $action 默认ajax触发的动作。
088 */
089 function open_ajax($action)
090 {
091 $this->is_ajax=true;
092 $this->ajax_action_name=$action;
093 }
094 /**
095 * 获取显示"下一页"的代码
096 *
097 * @param string $style
098 * @return string
099 */
100 function next_page($style='')
101 {
102 if($this->nowindex<$this->totalpage){
103    return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
104 }
105 return '<span class="'.$style.'">'.$this->next_page.'</span>';
106 }
107 
108 /**
109 * 获取显示“上一页”的代码
110 *
111 * @param string $style
112 * @return string
113 */
114 function pre_page($style='')
115 {
116 if($this->nowindex>1){
117    return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
118 }
119 return '<span class="'.$style.'">'.$this->pre_page.'</span>';
120 }
121 
122 /**
123 * 获取显示“首页”的代码
124 *
125 * @return string
126 */
127 function first_page($style='')
128 {
129 if($this->nowindex==1){
130       return '<span class="'.$style.'">'.$this->first_page.'</span>';
131 }
132 return $this->_get_link($this->_get_url(1),$this->first_page,$style);
133 }
134 
135 /**
136 * 获取显示“尾页”的代码
137 *
138 * @return string
139 */
140 function last_page($style='')
141 {
142 if($this->nowindex==$this->totalpage){
143       return '<span class="'.$style.'">'.$this->last_page.'</span>';
144 }
145 return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
146 }
147 
148 function nowbar($style='',$nowindex_style='')
149 {
150 $plus=ceil($this->pagebarnum/2);
151 if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
152 $begin=$this->nowindex-$plus+1;
153 $begin=($begin>=1)?$begin:1;
154 $return='';
155 for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
156 {
157    if($i<=$this->totalpage){
158     if($i!=$this->nowindex)
159         $return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
160     else
161        
补充:Web开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,