[分享]自行撰寫的分頁class

看板PHP作者 (bin)時間15年前 (2009/02/19 22:13), 編輯推噓3(301)
留言4則, 3人參與, 最新討論串1/1
下載網址:http://www.badongo.com/file/13466244 第二下載網址:http://kj-94.myweb.hinet.net/pager_1_0.zip 此為zip檔,內含三個檔案: 1.pager.php 主要拿來include的檔案 2.test_init.php 範例程式碼1 3.test.php 範例程式碼2 每個php內有使用說明 此為第一次分享的拙作,請各位高手大大多多包含 如果有任何建議或指教,非常歡迎 發現bug也請email至item.search@gmail.com 非常感謝。 怕這個檔案為病毒的話請看以下程式碼: 1.pager.php <?php /* * 程式名稱 : pager(分頁器) * 版本 : v1.0 * 使用環境 : PHP + MySQL * 作者 : sdcomputer(鬢角)@nttu * 最後更新 : 2009.2.19 * 使用說明 : 請在使用本類別之前先進行好對資料庫的連線,本類別將會利用session記錄某些資料。不適合者勿用。 * 使用本類別需具備基礎class相關使用知識。小弟所提供的範例以方便了解為導向,並不含任何資訊安全 * 上的防護,若要改用本範例檔請自行加強防護。 * * 本類別為小弟之拙作,有任何指教歡迎來信item.search@gmail.com,小弟將虛心受教。 * 此外,若發現bug也煩請email至小弟的信箱,小弟將會進一步做修正。 */ class pager { private $statement = ""; private $num_rows = 0; private $total_rows = 0; //basic function public function __get($name){ return $this->$name; } public function __set($name, $value){ $this->$name = $value; } //setting function public function set_statement($stmt){ //設定陳述式,像是SELECT * FROM xxx WHERE ooo $this->statement = $stmt; //屬性:void $result = mysql_query($stmt)or die(mysql_error()); $this->total_rows = mysql_num_rows($result); } public function set_num_rows($nr){ //設定一頁要呈現幾筆資料 $this->num_rows = $nr; //屬性:void } //opration function public function get_page($num){ //列出第幾頁的資料,回傳值為mysql_query指令的返回值 $page_start = ($num-1) * $this->num_rows; //屬性:query result $sql = $this->statement." LIMIT $page_start , ".$this->num_rows; $result = mysql_query($sql)or die(mysql_error()); rem_obj($this); return $result; } public function has_page($num){ //回傳是否存在第$num頁 $num = ($num-1) * $this->num_rows; //屬性:boolean if($num>$this->total_rows) return false; else return true; } public function how_many_pages(){ //回傳總共有幾頁 return ceil($this->total_rows/$this->num_rows); //屬性:int } } //other operation function rem_obj($obj){ //記憶設定 session_start(); //屬性:void $_SESSION['rem_pager'] = $obj; } function clone_pager(){ //屬性:class / boolean session_start(); //說明:利用session將已設定的class複製到新的變數。 if(!empty($_SESSION['rem_pager'])) //舉例: $a = new pager(); return clone $_SESSION['rem_pager']; // $a->set_statement("select....."); else // $a->set_num_rows(10); return false; // 在此頁點連結到b頁面 } // $b = clone_pager(); 至此可沿襲a的屬性。詳情請參閱範例檔。 ?> 2.test_init.php <?php require_once("connection.php"); //此為include連線檔,請自備。 require_once("pager.php"); //此為include pager程式碼 $a = new pager(); $a->set_statement("SELECT * FROM your_table WHERE some_column_value >1"); //↑設定陳述式 $a->set_num_rows(10); //設定每頁幾筆資料 $result = $a->get_page(1); //設定顯示第幾頁 $num = mysql_num_rows($result); for($i=0;$i<$num;$i++){ $row = mysql_fetch_assoc($result); echo $row['name']."<br/>"; //show出第i筆資料 } //以下為建立分頁之連結 for($i=1;$i<=$a->how_many_pages();$i++){ if($i<$a->how_many_pages()) echo "<a href=test2.php?page=$i> $i </a> | "; else echo "<a href=test2.php?page=$i> $i </a>"; } ?> 3.test2.php <?php require_once("connection.php"); require_once("pager.php"); if(!clone_pager()){ //假設session並未記錄到任何設定 echo "need init"; }else{ $b = clone_pager(); $result = $b->get_page($_GET['page']); $num = mysql_num_rows($result); for($i=0;$i<$num;$i++){ $row = mysql_fetch_assoc($result); echo $row['name']."<br/>"; } echo "<br/>"; //以下為建立分頁之連結 for($i=1;$i<=$b->how_many_pages();$i++){ if($i<$b->how_many_pages()) echo "<a href=test2.php?page=$i> $i </a> | "; else echo "<a href=test2.php?page=$i> $i </a>"; } } ?> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.117.250.221 ※ 編輯: sdcomputer 來自: 122.117.250.221 (02/20 00:27)

02/20 08:58, , 1F
同為分頁分享人,謝謝您的無私~^+++^~
02/20 08:58, 1F

02/21 09:13, , 2F
呵~謝謝^^~話說PHP的分頁真的很不方便呢~XD
02/21 09:13, 2F

02/21 10:45, , 3F
嗯嗯 去看一下 phpBB 的分頁 還蠻好用的
02/21 10:45, 3F

02/23 08:28, , 4F
我那支function版的小改一下就變ajax的分頁^~^
02/23 08:28, 4F
文章代碼(AID): #19dMa0ks (PHP)