本文目录一览:
ThinkPHP使用心得分享-分页类Page的用法
ThinkPHP中的Page类在ThinkPHP/Extend/Library/ORG/Util/Page.class.php中,所以使用前要引入Page类:
import('ORG.Util.Page');
Page类的引入
$db = M('abc'); // 实例化数据表abc
$where = array('id' => '2'); // 条件语句$where, 例如表中字段id的值为2
$count = $db->where($where)->count(); // 获取符合条件的数据总数count
$page = new Page($count, 10); // 实例化page类,传入数据总数和每页显示10条内容
$limit = $page->firstRow . ',' . $page->listRows; // 每页的数据数和内容$limit
$result = $db->where($where)->limit($limit)->select(); // 分页查询结果
$this->result = $result; // 赋值
$this->show = $page->show(); // 获取分页的底部信息
以上代码是分页类实现的基本语句,当然喜欢使用原生sql语句的朋友也可以配合原生sql语句实现查询分页:
import('ORG.Util.Page');
Page类的引入
$db = M('abc'); // 实例化数据表abc
$where = array('id' => '2'); // 条件语句$where, 例如表中字段id的值为2
$count = $db->where($where)->count(); // 获取符合条件的数据总数count
$page = new Page($count, 10); // 实例化page类,传入数据总数和每页显示10条内容
$Modle = new Model(); // 实例化新数据模型
$sql = 'select id, name from abc where ' . $where . ' limit ' . $page->firstRow . ',' . $page->listRows; // sql语句
$result = $Modle->query($sql); // 执行sql语句
$this->result = $result;
$this->show = $page->show();
当然,分布查询获取的内容也可以先对查询完的数据进行处理再赋值,比如
...
$result = $db->where($where)->limit($limit)->select(); // 分页查询结果
$res = abc($result); // abc方法(自定义方法或php函数)对结果$result进行数据排序或重组处理等
$this->result = $res; // 赋值
php分页功能怎么实现
php本身是没有分页概念的,分页是URL传参,然后通过mysql查询语句到数据库获取数据,然后实现的分页,url上的参数,通过PHP的$_GET都是可以获取到的。 现在市面上的PHP框架基本都有PHP分页类,参照文档直接调用就好了,如果想看实现过程,可以去下载一个TP框架,然后打开里面的分页类查看里面的源代码。
php分页使用方法
class Page
{
// 起始行数
public $firstRow;
// 列表每页显示行数
public $listRows;
// 页数跳转时要带的参数
public $parameter;
// 分页总页面数
protected $totalPages;
// 总行数
protected $totalRows;
// 当前页数
protected $nowPage;
// 分页的栏的总页数
protected $coolPages;
// 分页栏每页显示的页数
protected $rollPage;
// 分页参数
public $p;
// 分页显示定制
protected $config = array(
'header' => '条记录',
'prev' => '上一页',
'next' => '下一页',
'first' => '第一页',
'last' => '最后一页',
'theme' => ' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%'
);
/**
* 架构函数
* @access public
* @param array $totalRows 总的记录数
* @param array $listRows 每页显示记录数
* @param array $parameter 分页跳转的参数
*/
public function __construct($totalRows, $listRows, $p='p', $rollPage=5, $parameter='')
{
$this->p = $p;
$this->totalRows = $totalRows;
$this->parameter = $parameter;
$this->rollPage = $rollPage;
$this->listRows = !empty($listRows) ? $listRows : 15;
$this->totalPages = ceil($this->totalRows / $this->listRows); // 总页数
$this->coolPages = ceil($this->totalPages / $this->rollPage);
$this->nowPage = !empty($_GET[$this->p]) ? $_GET[$this->p] : 1;
if (!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
$this->nowPage = $this->totalPages;
}
$this->firstRow = $this->listRows * ($this->nowPage - 1);
}
public function setConfig($name, $value)
{
if (isset($this->config[$name])) {
$this->config[$name] = $value;
}
}
/**
* 分页显示输出
* @access public
*/
public function show()
{
if (0 == $this->totalRows) return '';
$nowCoolPage = ceil($this->nowPage / $this->rollPage);
$url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') ? '&' : "?") . $this->parameter;
$parse = parse_url($url);
if (isset($parse['query'])) {
parse_str($parse['query'], $params);
unset($params[$this->p]);
$url = $parse['path'] . '?' . http_build_query($params);
}
// 上下翻页字符串
$upRow = $this->nowPage - 1;
$downRow = $this->nowPage + 1;
if ($upRow > 0) {
$upPage = "<a href='" . $url . "&" . $this->p . "=$upRow'>{$this->config['prev']}</a>";
} else {
$upPage = "";
}
if ($downRow <= $this->totalPages) {
$downPage = "<a href='" . $url . "&" . $this->p . "=$downRow'>{$this->config['next']}</a>";
} else {
$downPage = "";
}
if ($nowCoolPage == 1) {
$theFirst = "";
$prePage = "";
} else {
$preRow = $this->nowPage - $this->rollPage;
$prePage = "<a href='" . $url . "&" . $this->p . "=$preRow'>上" . $this->rollPage . "页</a>";
$theFirst = "<a href='" . $url . "&" . $this->p . "=1'>{$this->config['first']}</a>";
}
if ($nowCoolPage == $this->coolPages) {
$nextPage = "";
$theEnd = "";
} else {
$nextRow = $this->nowPage + $this->rollPage;
$theEndRow = $this->totalPages;
$nextPage = "<a href='" . $url . "&" . $this->p . "=$nextRow'>下" . $this->rollPage . "页</a>";
$theEnd = "<a href='" . $url . "&" . $this->p . "=$theEndRow'>{$this->config['last']}</a>";
}
// 1 2 3 4 5
$linkPage = "";
for ($i = 1; $i <= $this->rollPage; $i++) {
$page = ($nowCoolPage - 1) * $this->rollPage + $i;
if ($page != $this->nowPage) {
if ($page <= $this->totalPages) {
$linkPage .= " <a href='" . $url . "&" . $this->p . "=$page'> " . $page . " </a>";
} else {
break;
}
} else {
if ($this->totalPages != 1) {
$linkPage .= " <span class='current'>{$page}</span>";
}
}
}
$pageStr = str_replace(
array('%header%', '%nowPage%', '%totalRow%', '%totalPage%', '%upPage%', '%downPage%', '%first%', '%prePage%', '%linkPage%', '%nextPage%', '%end%'),
array($this->config['header'], $this->nowPage, $this->totalRows, $this->totalPages, $upPage, $downPage, $theFirst, $prePage, $linkPage, $nextPage, $theEnd),
$this->config['theme']
);
return $pageStr;
}
}
具体调用方法
$count
是总记录数
15
是当前显示数
$p = new Page($count, 15);
$p->setConfig('header', '个项目'); // 可以设置变量名称,即输出的名称
$page = $p->show(); // 生成html page分页html
总的流程是 先读取出总的记录数 然后调用上面的分页类 在分页类里面输出2个变量一个是当前分页读取数据的开始,和偏移量
limit($p->listRows, $p->firstRow)
然后执行分页sql语句
$page = $p->show();
$page
就是标准分页html
Thinkphp数组分页如何操作
第一种:利用Page类和limit方法
$User = M('User'); // 实例化User对象
import('ORG.Util.Page'); // 导入分页类
$count = $User->where('status=1')->count(); // 查询满足要求的总记录数
$Page = new Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数
$show = $Page->show(); // 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
$list = $User->where('status=1')->order('create_time')->limit($Page->firstRow . ',' . $Page->listRows)->select();
$this->assign('list', $list); // 赋值数据集
$this->assign('page', $show); // 赋值分页输出
$this->display(); // 输出模板
第二种:分页类和page方法的实现
$User = M('User'); // 实例化User对象
// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
$list = $User->where('status=1')->order('create_time')->page($_GET['p'] . ',25')->select();
$this->assign('list', $list); // 赋值数据集
import("ORG.Util.Page"); // 导入分页类
$count = $User->where('status=1')->count(); // 查询满足要求的总记录数
$Page = new Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数
$show = $Page->show(); // 分页显示输出
$this->assign('page', $show); // 赋值分页输出
$this->display(); // 输出模板
带入查询条件
如果是POST方式查询,如何确保分页之后能够保持原先的查询条件呢,我们可以给分页类传入参数,方法是给分页类的parameter
属性赋值:
import('ORG.Util.Page'); // 导入分页类
$mapcount = $User->where($map)->count(); // 查询满足要求的总记录数
$Page = new Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数
// 分页跳转的时候保证查询条件
foreach ($map as $key => $val) {
$Page->parameter .= "$key=" . urlencode($val) . '&';
}
$show = $Page->show(); // 分页显示输出