>백엔드 개발 >PHP 튜토리얼 > 【代码】PHP 分页种

【代码】PHP 分页种

WBOY
WBOY원래의
2016-06-13 12:35:052157검색

【代码】PHP 分页类

下面是我自己写的分页类,可做简单的修改
[php]
<?php /**
* @name Pager's class
* @author Refer
* @copyright www.phpcq.com
* @version Alpha 1.0
* @todo 下拉菜单分页---From表单传&#20540;...可以根据自己的情况(采用GET传&#20540;)
*
*/
class Pager{
/**
* startRow
*
* @var Int
*/
public $startRow;
/**
* 页面的总数
*
* @var Int
*/
public $pageTotal;
/**
*翻页&#20540;
*
* @var Int
*/
public $offset;
/**
* 每页所显示记录数
*
* @var Int
*/
public $pageSize;
/**
* get参数
*
* @var Int
*/
public $get;
/**
* post参数
*
* @var Int
*/
public $post;
/**
* 构造函数--初始化翻页类 (PS:^_^不错的php学习交流群:276167802,验证:csl,有兴趣的话可以加入进来一起讨论)
*
* @param Int $total
* @param Int $pageSize
*/
public function __construct($total,$pageSize){
$this->get = (int)$_GET['startRow'];
$this->post = (int)$_POST['pageNums'];
$this->pageSize = $pageSize;
$this->pageTotal = ceil($total / $pageSize);
switch ($_GET['method']){
default:
$this->startRow = 1;
$this->offset = 0;
break;
case 'first':
$this->first();
break;
case 'previous':
$this->previous();
break;
case 'next':
$this->next();
break;
case 'last':
$this->last();
break;
}
}
/**
* 首页方法
*
*/
public function first(){
$this->startRow = 1;
$this->offset = ($this->startRow - 1) * $this->pageSize;
}
/**
* 上一页方法
*
*/
public function previous(){
$this->startRow = $this->get - 1;
if ($this->startRow startRow = 1;
}
$this->offset = ($this->startRow - 1) * $this->pageSize;
}
/**
* 下一页方法
*
*/
public function next(){
$this->startRow = $this->get + 1;
if ($this->startRow > $this->pageTotal) {
$this->startRow = $this->pageTotal;
}
$this->offset = ($this->startRow - 1) * $this->pageSize;
}
/**
* 最后页方法
*
*/
public function last(){
$this->startRow = $this->pageTotal;
$this->offset = ($this->startRow - 1) * $this->pageSize;
}
/**
* 下拉菜单翻页
*
* @return Int
*/
public function numPages(){
if ($this->post) {
$pageNums = "<option>$this->post</option>";
$this->startRow = $this->post;
$this->offset = ($this->post - 1) * $this->pageSize;
}
for ($i = 1; $i pageTotal;$i++){
if ($i != $this->post) {
$pageNums .= "<option>$i</option>";
}
}
return $pageNums;
}
/**
* 当前是第几页
*
* @return Int
*/
public function noncePage(){
if ($this->post) {
return $this->startRow = $this->post;
} else {
return $this->startRow = $this->startRow;
}
}
}


调用方法:
[php]
$total = $total->total();//总记录
$pageSize = 15;//每页显示的记录
$pager = new Pager($total,$pageSize);
if ($pager->post){$startRow = $pager->noncePage();}
$startRow = $pager->startRow;
$pager->numPages();
$article->query("SELECT * FROM `categories` as c,`articles` as n WHERE c.id = n.pid
ORDER BY n.id DESC LIMIT $pager->offset,$pageSize");
while ($article->fetchArray()) {
$article->data['content'] = mb_substr(strip_tags($article->data['content']),0,40,'utf-8');
$articleIndex[] = $article->data;
}
$article->free();
} else {
header("location:?act=login");
}
$view->view->assign('startRow',$startRow);//caption
$view->view->assign('total',$total);//caption
$view->view->assign('pageNums',$pager->numPages());//pageNums
$view->view->assign('noncePage',$pager->noncePage());//pageNums
$view->view->assign('pageTotal',$pager->pageTotal);//pageTotal

模板页面:
[php]
<!--{if $pageTotal > 1}-->

本文出自 “清源教育” 博客,转载请注明此处,谢谢!
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.