继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

php面向对象分页类代码

达令说
关注TA
已关注
手记 307
粉丝 22
获赞 120

<?php

//自定义分页类

class Page

{

public $page=1; //当前页

public $pageSize=5; //页大小

public $maxPage=0; //最大页数

public $maxRows=0; //最大数据条

public $url =null; //访问的url地址

public $params=null; //url的参数条件

public function __construct($maxRows,$pageSize=5){

$this->maxRows = $maxRows;

$this->pageSize= $pageSize;

//获取当前页

$this->page = isset($_GET['p'])?$_GET['p']:1;

$this->getMaxPage();

$this->checkPage();

//获取和封装url地址

$this->url = $_SERVER["PHP_SELF"];

$this->getParams();

}

//获取分页信息

public function limit(){

return (($this->page-1)*$this->pageSize).",".$this->pageSize;

}

//计算分页中的最大页数

private function getMaxPage(){

$this->maxPage = ceil($this->maxRows/$this->pageSize);

}

//判断当前页数是否越界

private function checkPage(){

if($this->page>$this->maxPage){

$this->page = $this->maxPage;

}

if($this->page<1){

$this->page=1;

}

}

//封装url上搜索条件

private function getParams(){

foreach($_GET as $k=>$v){

if($k!="p" && !empty($v)){

$this->params.="&{$k}={$v}";

}

}

}

//生成分页HTML代码

public function show(){

$str = "";

$str.= "当前第{$this->page}/{$this->maxPage}页 共计{$this->maxRows}条 ";

$str.= " <a href='{$this->url}?p=1{$this->params}'>首页</a> ";

$str.= " <a href='{$this->url}?p=".($this->page-1)."{$this->params}'>上一页</a> ";

$str.= " <a href='{$this->url}?p=".($this->page+1)."{$this->params}'>下一页</a> ";

$str.= " <a href='{$this->url}?p={$this->maxPage}{$this->params}'>末页</a> ";

return $str;

}

}


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP