-
-
/**
- * 簡單分頁類別
- * Page.class.php
- */
- class Page
- {
- private $page_num; //每頁顯示的資訊條數
- private $page_all_no; //訊息的總條數
- private $page_len; //顯示幾個頁碼
- private $page; //目前的頁數
- private $page_maxprivate $page_max ; //頁數最大值
- private $page_no_array; //頁數的陣列
- public $start_num; //查詢語句limit的起始值
- private $page_change; //在第幾個頁碼開始頁碼遞增
- private $URL; //取得目前頁面的URL
public function __construct($page_all_no, $page_num=5, $page_len=5)
- {
- $this->page_all_no = intval($page_all_no);
- $this->page_num = intval($page_num);
- $this->page_len = intval($page_len);
- $this-> URL = $_SERVER['REQUEST_URI'];
- $this->max_page(); //得到頁數的最大值
- $this->page_no(); //得到目前頁數
- $ this->page_no_array(); //頁數數組
- $this->start_num(); //得到sql語句中limit的起始值
- $this->change_page(); //得到遞增開始的頁碼$this->page_change
- $this->getURL(); //得到目前的URL並處理回傳
- }
private function isArray($str,$str_self )
- {
- if(!is_array($str)) throw new Exception("$str_self must be an Array type");
- }
private function page_no( )
- {
- $this->page = isset($_GET['page']) ? $_GET['page'] : 1;
- if(isset($_GET['page']) && $_GET['page'] page = 1;
- if(isset($_GET['page']) && $_GET['page'] > $this->page_max) $ this->page = $this->page_max;
- return $this->page;
- }
private function max_page()
- {
- return $this ->page_max = $this->page_all_no page_all_no/$this->page_all_no page_all_no/$this->page_num);
- }
private function change_page()
- {
- return $this->page_change = ceil($this->page_len / 2);
- }
private function page_no_array()
- {
- {
- return $this->page_no_array = range(1, $this->page_max);
- }
private function start_num()
- {
- return $this->start_num = $this->page_num * ($this->page - 1);
- }
private function getURL()
- {
- if(!empty($_SERVER ['argc']) ? $_SERVER['argc'] == 0 : strpos($_SERVER['REQUEST_URI'], '?') === false)
- {
- $this->URL = $this->URL.'?';
- }else{
- $url_a = "/?page=[0-9]{1,}/";
- $url_b = "/&page=[ 0-9]{1,}/";
- ereg("?page=[0-9]{1,}", $this->URL) ? $this->URL = preg_replace($url_a, " ?", $this->URL):
- ereg("&page=[0-9]{1,}", $this->URL) ? $this->URL = preg_replace($url_b, "&" , $this->URL):$this->URL = $this->URL.'&';
- }
- return $this->URL;
- }
- private function header_info($total_data_modifier='總數:', $total_page_modifier=' 總頁數:', $current_page_modifier=' 目前頁:', $header_info_modifier='', $header_data_color = 'redo'){
- if($this->page_max != 1 && $this->page_all_no != 0)
- {
- $header_info = $total_data_modifier.' '.$this->page_all_no.'';
- $header_info .= $total_page_modifier.''.$this ->page_max.'';
- $header_info .= $current_page_modifier.''.$this->page. ' '."rn";
- if(!empty($header_info_modifier)) $header_info = $header_info_modifier.$header_info;
- return $header_info;
- }else{
- return NULL;
- }
- }
private function first($first_format = '第一頁')
- {
- return $this->page = = 1 ? $first_format."rn" : ''. $first_format.''."rn";}
蕨函數last($last_format = '上一頁')
- {
- if($this->page == 1) return $last_format."rn ";
- 回傳$this->page-1 == 1 ? ''.$last_format.''. "rn" : ' '.$last_format.''."rn";
- }
真空函式page_num_format($separator=' ', $left_modifier='[', $right_modifier=']'、$both_sides=false、 $current_page_color = '紅色')
- {
- 空($separator) ? $separator = ' ' : $separator;
- $page_array = '';
- if($this->page_max page_len)
- {
- for ($i =0; $i $i )
- {
- if($this->page_no_array[$i] == $this->page)
- {
- $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn ";
- }else{
- if($i == 0)
- {
- $page_one = substr($this->URL,0,strlen($this->URL)- 1);
- $ the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
- }else{
- $the[$i] = ''。 $left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
- }
- }
- $page_array .= $the[$i].$分隔符號;
- }
- if($both_sides === false)
- {
- $page_array = substr($page_array,0 ,strrpos($page_array, $separator));
- }
- }
- } elseif ($both_sides === true){
- $page_array = $separator.$page_array;
- }else{
- 引發新的異常( '錯誤:$both_sides 必須是布林類型。');
- }
- }else{
- if($this->page page_change)
- {
- $i_start = 0;
- }else{
- $i_start = $this->page - $this->page_change;
- //如果最大的頁碼已顯示,那麼開始頁不會多了會遞增
- if($i_start >= $this->page_max - $ this->page_len){
- $i_start = $this->page_max - $this->page_len;
- }
- }
- $i_end = ($i_start $this->page_len) - 1;
- 對於($i = $i_start; $i {
- if($this->page_no_array[$i] == $this->page)
- {
- $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.' '."rn";
- }else{
- if($i == 0)
- {
- $page_one = substr($this->URL,0,strlen($this-> ;URL)-1);
- $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$ right_modifier.''."rn" ;
- }else{
- $the[$i] = '' .$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
- }
- }
- $page_array .= $the[$i]. $separator;
- }
- if($both_sides === false)
- {
- $page_array = substr( $page_array,0,strrpos($page_array, $separator));
- } elseif ($both_sides === true){
- $page_array = $separator.$page_array;
- }else{
- throw new Exception('錯誤:$both_sides必須是布林類型。');
- }
- }
- return $page_array;
- }
私有函數next($next_format = '下一頁')
- {
- if($ this->page >= $this->page_max) return $next_format."rn";
- return ' '.$next_format.' '."rn";
- }
private function end($end_format = '最後頁')
- {
- return $this->page > ;= $this->page_max ? $end_format."rn" : ''.$end_format.' '."rn";
- }
公用函式 select_page($target='self', $select_page_mode='PMA', $value_left_modifier='' , $value_right_modifier ='')
- {
- if($this->page_max != 1 && $this->page_all_no != 0)
- {
- if($select_page_mode === '正常')
- {
- $page_no_array = $this->page_no_array;
- }elseif($select_page_mode === 'PMA'){
- $page_no_array = $this->PMA_page_nono_array( ); {
- 引發新的例外('$select_page_mode 是未知模式');}
- $select_page = '';
- $select_page .= ''。 return $select_page;
- }else{
- return NULL;
- }
- }
原生函數PMA_page_no_array()
- {
- $showAll = 200 ;
- $sliceStart = 5;
- $sliceEnd = 5;
- $percent = 20;
- $range = 10;
-
if ($this->page_max $this->PMA_page_no_array = range(1, $this->page_max);
- } else {
- $ this->PMA_page_no_array = array();
- for ($ i = 1; $i page_max; $i ) {
- $this->PMA_page_no_array[] = $i;
- }
- $i = $sliceStart;
- $x = $this->page_max - $sliceEnd;
- $met_boundary = false;
- while ($i if ($i >= ($this->page - $range) && $i page $range)) {
- $i ;
- $ met_boundary = true;
- } else {
- $i = $i 樓層($this->page_max / $percent);
- if ($i > ($this->page - $range) ) && !$met_boundary) {
- $i = $this->page - $range;
- }
- }
- if ($i > 0 && $i $this->PMA_page_no_array[] = $i;
- }
- }
- sort($this->PMA_page_no_array);
- $this->PMA_page_no_array = array_unique($this->PMA_page_no_array);}PMA_page_no_array;
- }
public function key_change_page(){
- echo '< ;script type="text/javascript">
- document.onkeydown=Page_keypress ;
- function Page_keypress(e){
- if(!e)var e = window.event;
- var kc = e.which||e.keyCode;
- switch(kc){
- 案例37:
- if(document.getElementById("kl_last") != null) location.href=document.getElementById("kl_last ").href;
- 中斷;
- 案例39:
- if(document.getElementById("kl_next") != null) location.href=document.getElementById("kl_next").href;
- 中斷;
- }
- }
- ' ;
- }
public function eshow()
- {
- return $this->last().$this->next();
- } p>
public function showForHelp()
- {
- return $this->first('首頁', '').$this->last('上一頁', '') .$this->page_num_format('', '', '', false, '#FF8500 ').$this->next('下一頁', '').$this->end('尾頁' , '');
- }
- public function show()
- {
- return $this->header_info().$this->first('首頁', '').$this->last('上一頁', '').$this->page_num_format('', '', '', false, '#FF8500').$this->next('下一頁', '').$this- >end('尾頁', '');
- }
- }
-
-
-
- 複製程式碼
-
-
2,php分頁類別的使用方法:
-
- header('content-type:text/html;charset=utf-8');
- include('Page.class.php') ;
- $page_all_no = 12000; //資料的總條數
- $page_num = 25; //設定每頁顯示的條數
- $page_len = 7; //顯示最多的頁碼數
- $page = new Page($page_all_no, $page_num, $page_len);
- $start_num = $page->start_num;
- $sql = "select * from table limit {$start_num}, {$page_num} ";
- $page->key_change_page(); //方向鍵翻頁
- var_dump($sql);
- //自帶三種分頁形式,可再按需要新增方法,像下面那樣呼叫
- echo '
'.$page->select_page('self', 'NORMAL').' '; //這裡的第二個參數預設為PMA模式,兩種模式差異在上面已給出
- echo '
'.$page->eshow().' ';
- echo '
'.$page->show().' ';
複製程式碼
3,php分頁類,效果圖:
|