本篇原始出處: 一陣風,讓我翱翔天空~~


這個能產生分頁導覽的 class 是前年我所寫的,
最近又拿出來改寫了一下,其效果如下圖:

prev << <.. 21 22 23 24 25 26 27 28 29 30 ..> >> next

/*
歡迎轉載,但轉載時請保留此宣告,並不得作為商業用途
作者: 翔
來源網站: http://blog.xuite.net/samba2005/blog
*/

所產生的是一個純文字的導覽列,
類別的建立只要輸入:目前所在頁數、資料總數、每頁顯示資料筆數、導覽列顯示頁數。

Class程式內容:
01 <?php
02 class SplitPage {
03   var $records_per_page;  // 顯示筆數
04   var $page;                      // 目前所在頁數 
05   var $total_records;         // 資料總數
06   var $total_pages;            // 總分頁數
07   var $started_record;
08   var $listPage;
09   var $startPage;
10   var $endPage;
11   var $viewlist;
12
13   function SplitPage($page, $total_records, $records_per_page, $listPage) { //物件建構
14     $this->records_per_page = $records_per_page;
15     $this->page                      = $page;
16     $this->total_records         = $total_records;
17     $this->listPage                 = $listPage;
18     $this->setALL();
19   }
20
21   function setALL() {  //設定類別參數
22     $this->total_pages      = ceil($this->total_records / $this->records_per_page);
23     $this->started_record = $this->records_per_page * ($this->page-1);
24     if($this->listPage < $this->total_pages) {
25       if($this->page % $this->listPage == 0)
26         $this->startPage = $this->page - $this->listPage + 1;
27       else
28         $this->startPage = $this->page - $this->page % $this->listPage + 1;
29
30       if(($this->startPage + $this->listPage) > $this->total_pages)
31         $this->endPage = $this->total_pages;
32       else
33         $this->endPage = ($this->startPage + $this->listPage - 1);
34     }
35     else {
36       $this->startPage = 1;
37       $this->endPage = $this->total_pages;
38     }
39   }
40
41   function setViewList($url, $target = false) //產生導覽列
42     if($target) $temp_target = " target='{$target}'";
43     $this->viewlist = '';
44     if($this->total_pages > 1) {
45       if(($this->page - 1) != 0) {
46         $this->viewlist .= "<a href='{$url}page=" . ($this->page - 1) ."'{$target}>prev</a> ";
47         if($this->total_pages > $this->listPage)
48           if(($this->startPage - $this->listPage) >= 1 and $this->page > $this->listPage) {
49             $this->viewlist .= "<a href='{$url}page=1'{$target}>&lt;&lt;</a> ";
50             $this->viewlist .= "<a href='{$url}page=" . ($this->startPage - $this->listPage) . "'{$target}>&lt;..</a> ";
51           }
52       }
53
54       for($i = $this->startPage; $i <= $this->endPage; $i++)
55         if($i != $this->page)
56           $this->viewlist .= "<a href='{$url}page={$i}'{$target}>{$i}</a> ";
57         else
58           $this->viewlist .= "<b>{$i}</b> ";
59
60       if(($this->page + 1) <= $this->total_pages) {
61         if(($this->total_pages > $this->listPage) and ($this->endPage != $this->total_pages)){
62           $this->viewlist .= "<a href='{$url}page=" . ($this->endPage + 1) . "'{$target}>..&gt;</a> ";
63           $this->viewlist .= "<a href='{$url}page={$this->total_pages}'{$target}>&gt;&gt;</a> ";
64         }
65         $this->viewlist .= "<a href='{$url}page=" . ($this->page + 1) . "'{$target}>next</a>";
66       }
67    
}
68  
}
69 }
70

 

使用範例
01 <?php
02 $sql = "select count(log_id) as total from table1";  //統計資料庫內欲查詢的資料總筆數
...
...略...
...

10 if(isset($_GET['page']) and $_GET['page'] != 0 and is_numeric($_GET['page']))  //設定目前頁數
11   $nowPage = $_GET['page'];
12 else
13   $nowPage = 1;
14 $page = new SplitPage($nowPage, $row['total'], 15, 20);  //建構出 SplitPage 物件
15 $page->setViewList("test.php?", 'test');  //設定導覽列資料,參數1為連結的頁面,參數2為連結的target(本參數可不設定)
16 $showdata['viewlist'] = $page->viewlist;  // $page->viewlist 即為所產生的導覽列,就如同上圖所示.
...
...略...
...

21  $sql = "select * from table1 limit {$page->started_record}, {$page->records_per_page}";  //每頁顯示資料的 sql 語法,僅取出所需的資料筆數.
...
...略...
...

31 ?>

 

歷史上的今天:
arrow
arrow
    全站熱搜

    正義的胖虎 發表在 痞客邦 留言(0) 人氣()