讓人崩潰的 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         $return.=$this->_get_text('<span class="'.$nowindex_style.'">'.$i.'</span>');
162    }else{
163     break;
164    }
165    $return.="\n";
166 }
167 unset($begin);
168 return $return;
169 }
170 /**
171 * 獲取顯示跳轉按鈕的代碼
172 *
173 * @return string
174 */
175 function select($url)
176 {
177 $return='<select name=";PB_Page_Select" >';
178 for($i=1;$i<=$this->totalpage;$i++)
179 {
180    if($i==$this->nowindex){
181     $return.='<option value='.$url.$i.' selected>'.$i.'</option>';
182    }else{
183     $return.='<option value='.$url.$i.'>'.$i.'</option>';
184    }
185 }
186 unset($i);
187 $return.='</select>';
188 return $return;
189 }
190
191 /**
192 * 獲取mysql 語句中limit需要的值
193 *
194 * @return string
195 */
196 function offset()
197 {
198 return $this->offset;
199 }
200
201 /**
202 * 控制分頁顯示風格(你可以增加相應的風格)
203 *
204 * @param int $mode
205 * @return string
206 */
207 function show($mode=1,$url='')
208 {
209 switch ($mode)
210 {
211    case '1':
212     $this->next_page='下一頁';
213     $this->pre_page='上一頁';
214     return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select($url).'頁';
215     break;
216    case '2':
217     $this->next_page='下一頁';
218     $this->pre_page='上一頁';
219     $this->first_page='首頁';
220     $this->last_page='尾頁';
221     return $this->first_page().$this->pre_page().'[第'.$this->nowindex.' 頁]'.$this->next_page().$this->last_page().'第 '.$this->select($url).'頁';
222     break;
223    case '3':
224     $this->next_page='下一頁';
225     $this->pre_page='上一頁';
226     $this->first_page='首頁';
227     $this->last_page='尾頁';
228     return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
229     break;
230    case '4':
231     $this->next_page='下一頁';
232     $this->pre_page='上一頁';
233     return $this->pre_page().$this->nowbar().$this->next_page();
234     break;
235    case '5':
236     return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
237     break;
238 }
239
240 }
241 /*----------------private function (私有方法)-----------------------------------------------------------*/
242 /**
243 * 設置url頭地址
244 * @param: String $url
245 * @return boolean
246 */
247 function _set_url($url="")
248 {
249 if(!empty($url)){
250       //手動設置
251    $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
252 }else{
253       //自動獲取
254    if(empty($_SERVER['QUERY_STRING'])){
255        //不存在QUERY_STRING時
256     $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
257    }else{
258        //
259     if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
260         //地址存在頁面參數
261      $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
262      $last=$this->url[strlen($this->url)-1];
263      if($last=='?'||$last=='&'){
264          $this->url.=$this->page_name."=";
265      }else{
266          $this->url.='&'.$this->page_name."=";
267      }
268     }else{
269         //
270      $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
271     }//end if  
272    }//end if
273 }//end if
274 }
275
276 /**
277 * 設置當前頁面
278 *
279 */
280 function _set_nowindex($nowindex)
281 {
282 if(empty($nowindex)){
283    //系統獲取
284  
285    if(isset($_GET[$this->page_name])){
286     $this->nowindex=intval($_GET[$this->page_name]);
287    }
288 }else{
289       //手動設置
290    $this->nowindex=intval($nowindex);
291 }
292 }
293
294 /**
295 * 爲指定的頁面返回地址值
296 *
297 * @param int $pageno
298 * @return string $url
299 */
300 function _get_url($pageno=1)
301 {
302 return $this->url.$pageno;
303 }
304
305 /**
306 * 獲取分頁顯示文字,比如說默認情況下_get_text('<a href="">1</a>')將返回[<a href="">1</a>]
307 *
308 * @param String $str
309 * @return string $url
310 */
311 function _get_text($str)
312 {
313 return $this->format_left.$str.$this->format_right;
314 }
315
316 /**
317    * 獲取鏈接地址
318 */
319 function _get_link($url,$text,$style=''){
320 $style=(empty($style))?'':'class="'.$style.'"';
321 if($this->is_ajax){
322       //如果是使用AJAX模式
323    return '<a '.$style.' href="javascript:'.$this->ajax_action_name.'(\''.$url.'\')">'.$text.'</a>';
324 }else{
325    return '<a '.$style.' href="'.$url.'">'.$text.'</a>';
326 }
327 }
328 /**
329    * 出錯處理方式
330 */
331 function error($function,$errormsg)
332 {
333      die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b> :'.$errormsg);
334 }
335 }
336 ?>
發佈了45 篇原創文章 · 獲贊 13 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章