PHP封裝類 【 設置分頁 】 !!! 能夠直接引用 !!! 都有本身理解的註釋,挺詳細的,有搜到的朋友能夠能幫到大家 【 新手一看練兩遍就懂 】

 

 

在網頁要顯示出的內容,就是客戶能看到的東西javascript

 

 

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>無標題文檔</title>
 6 </head>
 7 
 8 
 9 <body>
 10 <?php  11 
 12 
 13 我的總結:  14 封裝類 DBDA.class.php  15 count(*) 統計方法 統計函數  16 StrQuery 統計函數,或者我只求出一個值來,這個關鍵字它一樣會給我統計出全部函數  17 
 18 
 19 
 20 
 21 
 22 include("DBDA.class.php"); //引入另外一個網頁代碼
 23 $db = new DBDA();  //造一個對象 DBDA
 24 
 25 
 26 /*$sql = "select * from info";  27 var_dump($db->Query($sql)); //返回查詢語句  28 */
 29 
 30 /*$sql = "select * from info";  31 var_dump($db->GuanQuery($sql)); //返回關聯數組  32 */
 33 
 34 
 35 /*$sql = "select * from info";  36 var_dump($db->GuanQuery($sql)); //返回拼接的字符串  37 */
 38 
 39 
 40 
 41 $sql = "select password from login where username='zhangsan'";  42 
 43 echo $db->StrQuery($sql);   //統計函數  44 
 45 //$sql = "update info set name='楊寧波' where code='p001'"; //  46 
 47 //var_dump($db->Query($sql,0));  48 
 49 //一個字符串裏面包含全部查到的數據  50 //n001^漢族|n002^回族|n003^維吾爾族|  51 
 52 
 53 
 54 //共10頁 當前第5頁 5/10 首頁 上一頁 1 2 3 4 5 6 7 8 9 10 下一頁 尾頁 跳轉到 【】 頁
 55 /*
 56 0 -- <div>共10頁</div>  57 1 -- <div>當前第5頁</div>  58 2 -- <input type='button' value='上一頁' />  59 3 -- <span>1</span><span>1</span><span>1</span>  60 4 -- <input type='button' value='下一頁' />  61 5 -- 跳轉<input type='text' /> 頁  62 
 63 $html = array();  64 fpage(4,2);  65 */
 66 
 67 //下面是設置分頁的引用和運行方法 上面的一些內容都得要註釋掉!/* */
 68 
 69 include("DBDA.class.php"); //引入另外一個網頁代碼
 70 $db = new DBDA();  //造一個對象 DBDA
 71 include("page.class.php");  //引入page.class.php這個網頁代碼
 72 
 73 $sz = "select count(*) from chinastates";   //求總條數
 74 $zts = $db->StrQuery($sz);  75 
 76 
 77 //造分頁類對象 每頁顯示20條數據
 78 $page = new Page($zts,20);  79 
 80 /* 81 include("page.class.php"); 82 $page = new Page(數據總條數,每頁顯示幾條); 83 */
 84 
 85 $sql = "select * from chinastates ".$page->limit; //講這兩個關鍵字拼接在後面就能實現分頁!!
 86 
 87 $attr = $db->Query($sql);  88 
 89 
 90 //<table></table>輸出表單型,
 91 echo "<table width='100%' border='1' cellpadding='0' cellspacing='0'>";  92 
 93 foreach($attr as $v)  //循環輸出每一行
 94 {  95     echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$v[2]}</td></tr>";  96 }  97 
 98 echo "</table>"; //輸出表單型
 99 
100 echo $page->fpage(); // page方法裏面有個fpage將它輸出。 101 
102 //基本上就是顯示出下面這樣: 103 //共100條記錄 本頁10條 本頁從 25—35 條 首頁 上一頁 1 2 3 4 5 6 7 8 9 10 下一頁 末頁 跳轉到【】(頁)GO
105 ?> 106 </body> 107 </html>

 

 

 

封裝類, 純處理頁面,客戶是看不到的,在後臺運行的php

 

 1 <?php  2 //我的總結
 3 reslut 意思:結果  4 字符長度關鍵字 strlen 
 5 截取字符串關鍵字 substr 
 6 
 7 class DBDA   //PHP連接MySQL先定義一個類
 8 {  9     //分裝下面這四個類名字能夠隨便取
 10     public $host="localhost";    //pudlic 公衆的,給IP地址一個接受的名 $host
 11     public $uid="root";   //mysql軟件名,給軟件名一個接受的名 $uid
 12     public $pwd="123";    //mysql軟件密碼,給密碼一個接受的名 $pwd
 13     public $dbname="mydb";   //要連的數據庫名
 14     
 15     /**  16  *給一個sql語句,返回執行的結果  17  *@param string $sql 用戶指定的sql語句  18  *@param int $type 用戶給的語句類型,0表明增刪改,1表明查詢  19  *@return 返回查詢的結果,若是是查詢返回二維數組,若是是增刪改返回true或false  20     */
 21                       //$type 表明是給的SQL語句是什麼類型的數據  22  //通常網站上查詢用的比較多,就給$type一個默認值 1
 23     function Query($sql,$type=1)  //上面有註釋詳解 要養成這個習慣「寫註釋」
 24  {  25         //造鏈接對象
 26         $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);  27         
 28         //執行sql語句
 29         $reslut = $db->query($sql);  30         
 31         //從結果集對象裏面取數據
 32         if($type==1)  33  {  34             return $reslut->fetch_all();  //reslut調用fetch_all直接返回數組
 35  }  36         else   //若是
 37  {  38             return $reslut;   //上面有註釋
 39  }  40  }  41     
 42     /**  43  *給一個sql語句,返回關聯的二維數組  44  *@param string $sql 用戶指定的sql語句  45  *@param int $type 用戶給的語句類型,0表明增刪改,1表明查詢  46  *@return 返回查詢的結果,若是是查詢返回二維數組,若是是增刪改返回true或false  47     */
 48     function GuanQuery($sql,$type=1)  49  {  50         //造鏈接對象
 51         $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);  52         
 53         //執行sql語句
 54         $reslut = $db->query($sql);  55         
 56         //取數據
 57         if($type==1)  //先判斷type是否是等於一
 58  {  59             $attr = array();  //給一個空的數組
 60             while($a = $reslut->fetch_assoc()) //用while循環把全部的數據都取一遍。 $reslut->fetch_assoc()這句是取當前的數組,給a來接收一下
 61  {  62                 $attr[] = $a;  //給把$a接收的值交個 $attr[] 它就存了一個關聯數組了
 63  }  64             
 65             return $attr;    //把attr輸出 ,就返回了一個關聯數組
 66  }  67         else
 68  {  69             return $reslut;  //若是其餘數據就輸出 取當前的數組
 70  }  71  }  72     /**  73  *給一個sql語句,返回字符串  74  *@param string $sql 用戶指定的sql語句  75  *@param int $type 用戶給的語句類型,0表明增刪改,1表明查詢  76  *@return 返回查詢的結果,若是是查詢返回字符串,若是是增刪改返回true或false  77     */
 78     function StrQuery($sql,$type=1)  79  {  80         //造鏈接對象
 81         $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);  82         
 83         //執行sql語句
 84         $reslut = $db->query($sql);  85         
 86         //取數據
 87         if($type==1)   //先判斷type是否是等於一
 88  {  89             $attr = $reslut->fetch_all();   //取到全部數據,取到的是二維數組,需轉換成字符串
 90             $str="";  //讓str默認爲空 循環裏面的$str. 讓它加一 php裏面的加法用 . 來表明
 91             foreach($attr as $v)  //先取出一維數組
 92  {  93                 $str .= implode("^",$v);  //用implode關鍵字將$v取出的一維數組和"^"特殊符號拼接起來。 由於"^"符號在php裏基本用不上,因此用它。
 94                 $str .="&nbsp;|&nbsp;";   //每一次循環結束以前加上一個分隔符
 95  }  96             return substr($str,0,strlen($str)-1);  //截取字符串關鍵字 substr $ste收到的值從零開始,字符長度關鍵字 strlen 講$st取到的值再最後減一,最後那個多出來的分割符就去掉了!!
 97  }  98         else
 99  { 100             return $reslut; 101  } 102  } 103 }

 

 

純處理頁面    這個頁面是別人設計好的,引用一下,此內容是顯示分頁的。  分頁的設置html

 

 

  1 <?php 2     /** 3 file: page.class.php 4 完美分頁類 Page 5 */
  6     class Page { 7         private $total;                            //數據表中總記錄數
  8         private $listRows;                         //每頁顯示行數
  9         private $limit;                            //SQL語句使用limit從句,限制獲取記錄個數
 10         private $uri;                              //自動獲取url的請求地址
 11         private $pageNum;                          //總頁數
 12         private $page;                            //當前頁 
 13         private $config = array( 14                 'head' => "條記錄", 
 15                 'prev' => "上一頁", 
 16                 'next' => "下一頁", 
 17                 'first'=> "首頁", 
 18                 'last' => "末頁"
 19 ); 20         //在分頁信息中顯示內容,能夠本身經過set()方法設置
 21         private $listNum = 10;                     //默認分頁列表顯示的個數
 22 
 23         /** 24 構造方法,能夠設置分頁類的屬性 25 @param int $total 計算分頁的總記錄數 26 @param int $listRows 可選的,設置每頁須要顯示的記錄數,默認爲25條 27 @param mixed $query 可選的,爲向目標頁面傳遞參數,能夠是數組,也能夠是查詢字符串格式 28 @param bool $ord 可選的,默認值爲true, 頁面從第一頁開始顯示,false則爲最後一頁 29 */
 30         public function __construct($total, $listRows=25, $query="", $ord=true){ 31             $this->total = $total; 32             $this->listRows = $listRows; 33             $this->uri = $this->getUri($query); 34             $this->pageNum = ceil($this->total / $this->listRows); 35             /*如下判斷用來設置當前面*/
 36             if(!empty($_GET["page"])) { 37                 $page = $_GET["page"]; 38             }else{ 39                 if($ord) 40                     $page = 1; 41                 else
 42                     $page = $this->pageNum; 43 } 44 
 45             if($total > 0) { 46                 if(preg_match('/\D/', $page) ){ 47                     $this->page = 1; 48                 }else{ 49                     $this->page = $page; 50 } 51             }else{ 52                 $this->page = 0; 53 } 54             
 55             $this->limit = "LIMIT ".$this->setLimit(); 56 } 57 
 58         /** 59 用於設置顯示分頁的信息,能夠進行連貫操做 60 @param string $param 是成員屬性數組config的下標 61 @param string $value 用於設置config下標對應的元素值 62 @return object 返回本對象本身$this, 用於連慣操做 63 */
 64         function set($param, $value){ 65             if(array_key_exists($param, $this->config)){ 66                 $this->config[$param] = $value; 67 } 68             return $this; 69 } 70         
 71         /* 不是直接去調用,經過該方法,可使用在對象外部直接獲取私有成員屬性limit和page的值 */
 72         function __get($args){ 73             if($args == "limit" || $args == "page") 74                 return $this->$args; 75             else
 76                 return null; 77 } 78         
 79         /** 80 按指定的格式輸出分頁 81 @param int 0-7的數字分別做爲參數,用於自定義輸出分頁結構和調整結構的順序,默認輸出所有結構 82 @return string 分頁信息內容 83 */
 84         function fpage(){ 85             $arr = func_get_args(); 86 
 87             $html[0] = "<span class='p1'>&nbsp;共<b> {$this->total} </b>{$this->config["head"]}&nbsp;</span>"; 88             $html[1] = "&nbsp;本頁 <b>".$this->disnum()."</b> 條&nbsp;"; 89             $html[2] = "&nbsp;本頁從 <b>{$this->start()}-{$this->end()}</b> 條&nbsp;"; 90             $html[3] = "&nbsp;<b>{$this->page}/{$this->pageNum}</b>頁&nbsp;"; 91             $html[4] = $this->firstprev(); 92             $html[5] = $this->pageList(); 93             $html[6] = $this->nextlast(); 94             $html[7] = $this->goPage(); 95             
 96             $fpage = '<div style="font:12px \'\5B8B\4F53\',san-serif;">'; 97             if(count($arr) < 1) 98                 $arr = array(0, 1,2,3,4,5,6,7); 99             
100             for($i = 0; $i < count($arr); $i++) 101                 $fpage .= $html[$arr[$i]]; 102         
103             $fpage .= '</div>'; 104             return $fpage; 105 } 106         
107         /* 在對象內部使用的私有方法,*/
108         private function setLimit(){ 109             if($this->page > 0) 110                 return ($this->page-1)*$this->listRows.", {$this->listRows}"; 111             else
112                 return 0; 113 } 114 
115         /* 在對象內部使用的私有方法,用於自動獲取訪問的當前URL */
116         private function getUri($query){ 117             $request_uri = $_SERVER["REQUEST_URI"]; 118             $url = strstr($request_uri,'?') ? $request_uri :  $request_uri.'?'; 119             
120             if(is_array($query)) 121                 $url .= http_build_query($query); 122             else if($query != "") 123                 $url .= "&".trim($query, "?&"); 124         
125             $arr = parse_url($url); 126 
127             if(isset($arr["query"])){ 128                 parse_str($arr["query"], $arrs); 129                 unset($arrs["page"]); 130                 $url = $arr["path"].'?'.http_build_query($arrs); 131 } 132             
133             if(strstr($url, '?')) { 134                 if(substr($url, -1)!='?') 135                     $url = $url.'&'; 136             }else{ 137                 $url = $url.'?'; 138 } 139             
140             return $url; 141 } 142 
143         /* 在對象內部使用的私有方法,用於獲取當前頁開始的記錄數 */
144         private function start(){ 145             if($this->total == 0) 146                 return 0; 147             else
148                 return ($this->page-1) * $this->listRows+1; 149 } 150 
151         /* 在對象內部使用的私有方法,用於獲取當前頁結束的記錄數 */
152         private function end(){ 153             return min($this->page * $this->listRows, $this->total); 154 } 155 
156         /* 在對象內部使用的私有方法,用於獲取上一頁和首頁的操做信息 */
157         private function firstprev(){ 158             if($this->page > 1) { 159                 $str = "&nbsp;<a href='{$this->uri}page=1'>{$this->config["first"]}</a>&nbsp;"; 160                 $str .= "<a href='{$this->uri}page=".($this->page-1)."'>{$this->config["prev"]}</a>&nbsp;"; 161                 return $str; 162 } 163 
164 } 165     
166         /* 在對象內部使用的私有方法,用於獲取頁數列表信息 */
167         private function pageList(){ 168             $linkPage = "&nbsp;<b>"; 169             
170             $inum = floor($this->listNum/2); 171             /*當前頁前面的列表 */
172             for($i = $inum; $i >= 1; $i--){ 173                 $page = $this->page-$i; 174 
175                 if($page >= 1) 176                     $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a>&nbsp;"; 177 } 178             /*當前頁的信息 */
179             if($this->pageNum > 1) 180                 $linkPage .= "<span style='padding:1px 2px;background:#BBB;color:white'>{$this->page}</span>&nbsp;"; 181             
182             /*當前頁後面的列表 */
183             for($i=1; $i <= $inum; $i++){ 184                 $page = $this->page+$i; 185                 if($page <= $this->pageNum) 186                     $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a>&nbsp;"; 187                 else
188                     break; 189 } 190             $linkPage .= '</b>'; 191             return $linkPage; 192 } 193 
194         /* 在對象內部使用的私有方法,獲取下一頁和尾頁的操做信息 */
195         private function nextlast(){ 196             if($this->page != $this->pageNum) { 197                 $str = "&nbsp;<a href='{$this->uri}page=".($this->page+1)."'>{$this->config["next"]}</a>&nbsp;"; 198                 $str .= "&nbsp;<a href='{$this->uri}page=".($this->pageNum)."'>{$this->config["last"]}</a>&nbsp;"; 199                 return $str; 200 } 201 } 202 
203         /* 在對象內部使用的私有方法,用於顯示和處理表單跳轉頁面 */
204         private function goPage(){ 205                 if($this->pageNum > 1) { 206                 return '&nbsp;<input style="width:20px;height:17px !important;height:18px;border:1px solid #CCCCCC;" type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'page=\'+page+\'\'}" value="'.$this->page.'"><input style="cursor:pointer;width:25px;height:18px;border:1px solid #CCCCCC;" type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'page=\'+page+\'\'">&nbsp;'; 207 } 208 } 209 
210         /* 在對象內部使用的私有方法,用於獲取本頁顯示的記錄條數 */
211         private function disnum(){ 212             if($this->total > 0){ 213                 return $this->end()-$this->start()+1; 214             }else{ 215                 return 0; 216 } 217 } 218 } 219 
220     
221     
222  
相關文章
相關標籤/搜索