mysql分页请求参数封装_资源(1)----封装类(连接数据库mysql,分页)

1 <?php2 /**3 file: page.class.php4 完美分页类 Page5 */

6 classPage {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 functionfpage(){85 $arr = func_get_args();86

87 $html[0] = " 共 {$this->total} {$this->config["head"]} ";88 $html[1] = " 本页 ".$this->disnum()." 条 ";89 $html[2] = " 本页从 {$this->start()}-{$this->end()} 条 ";90 $html[3] = " {$this->page}/{$this->pageNum}页 ";91 $html[4] = $this->firstprev();92 $html[5] = $this->pageList();93 $html[6] = $this->nextlast();94 $html[7] = $this->goPage();95

96 $fpage = '

';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 .= '

';104 return $fpage;105 }106

107 /*在对象内部使用的私有方法,*/

108 private functionsetLimit(){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 functionstart(){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 functionfirstprev(){158 if($this->page > 1) {159 $str = " {$this->config["first"]} ";160 $str .= "{$this->config["prev"]} ";161 return $str;162 }163

164 }165

166 /*在对象内部使用的私有方法,用于获取页数列表信息*/

167 private functionpageList(){168 $linkPage = " ";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 .= "{$page} ";177 }178 /*当前页的信息*/

179 if($this->pageNum > 1)180 $linkPage .= "{$this->page} ";181

182 /*当前页后面的列表*/

183 for($i=1; $i <= $inum; $i++){184 $page = $this->page+$i;185 if($page <= $this->pageNum)186 $linkPage .= "{$page} ";187 else

188 break;189 }190 $linkPage .= '';191 return $linkPage;192 }193

194 /*在对象内部使用的私有方法,获取下一页和尾页的操作信息*/

195 private functionnextlast(){196 if($this->page != $this->pageNum) {197 $str = " {$this->config["next"]} ";198 $str .= " {$this->config["last"]} ";199 return $str;200 }201 }202

203 /*在对象内部使用的私有方法,用于显示和处理表单跳转页面*/

204 private functiongoPage(){205 if($this->pageNum > 1) {206 return '  ';207 }208 }209

210 /*在对象内部使用的私有方法,用于获取本页显示的记录条数*/

211 private functiondisnum(){212 if($this->total > 0){213 return $this->end()-$this->start()+1;214 }else{215 return 0;216 }217 }218 }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值