PHP封装类 【 设置分页 】 !!! 可以直接引用 !!! 都有自己理解的注释,挺详细的,有搜到的朋友可以能帮到你们 【 新手一看练两遍就懂 】...

 

 

在网页要显示出的内容,就是客户能看到的东西

 

 

  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>

 

 

 

封装类, 纯处理页面,客户是看不到的,在后台运行的

 

  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 }

 

 

纯处理页面    这个页面是别人设计好的,引用一下,此内容是显示分页的。  分页的设置

 

 

  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" οnkeydοwn="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" οnclick="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  

 

转载于:https://www.cnblogs.com/zc290987034/p/6028178.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值