文件名:page.impl.php
<?php
/**
* 分页类
*/
class PageImpl {
private $sql;
private $page;
private $totalRecord;
private $totalPage;
private $nextPage;
private $prePage;
private $pageSize;
public function __construct($sql, $page, $size=20) {
$this->sql = $sql;
$this->page = $page ? $page : 1;
$this->pageSize = $size;
}
public function queryResult($dbAdapter) {
$this->filterSql();
$result[0] = $dbAdapter->fetchAll($this->sql);
$pageSql = "SELECT FOUND_ROWS() as count";
$pageResult = $dbAdapter->fetchAll($pageSql);
$this->totalRecord = $pageResult[0]->count;
$result[1] = $this->pageResult();
return $result;
}
private function pageResult() {
if (!$this->pageList()) {
return null;
}
$pages = array();
$pages['page'] = $this->page;
$pages['total_page'] = $this->totalPage;
$pages['total_record'] = $this->totalRecord;
$pages['next_page'] = $this->nextPage;
$pages['pre_page'] = $this->prePage;
$pages['page_range'] = $this->pageRange(10);
return $pages;
}
private function filterSql() {
if ($this->page == 1) {
$this->sql .= " LIMIT 0," . $this->pageSize ;
}else {
$this->sql .= " LIMIT " . (($this->page - 1)*$this->pageSize) . "," . $this->pageSize;
}
$this->sql = preg_replace("/^select/i", "SELECT SQL_CALC_FOUND_ROWS", $this->sql);
}
private function pageList() {
if (!$this->totalRecord) {
return false;
}
if (($this->totalRecord % $this->pageSize) == 0) {
$this->totalPage = (integer)($this->totalRecord / $this->pageSize);
}else {
$this->totalPage = (integer)($this->totalRecord / $this->pageSize + 1);
}
if ($this->page > $this->totalPage) {
return false;
}elseif ($this->page < $this->totalPage) {
$this->nextPage = $this->page + 1;
}else {
$this->page = $this->totalPage;
}
if ($this->page > 1) {
$this->prePage = $this->page - 1;
}else {
$this->prePage = 1;
}
return true;
}
private function pageRange($range) {
if (2 > $range) {
$range = 2;
}
$halfRange = ceil($range / 2);
$newRange = $halfRange * 2;
$tmp = ($halfRange > $this->page) ? $newRange : $this->page + $halfRange;
$this->totalPage = $this->totalPage ? $this->totalPage : 1;
if ($this->page > $this->totalPage) {
$this->page = $this->totalPage;
}
$max = ($this->totalPage <= $tmp) ? $this->totalPage : $tmp;
$min = ($this->totalPage <= $tmp) ? (($newRange < $max) ? $max-($newRange-1) : 1) : (($halfRange > $this->page) ? 1 : $this->page - $halfRange + 1);
return range($min, $max);
}
}
2.通过sql语句,获取数据进行分页friend.php
class Friend extends Zend_Db_Table{
protected $_name = 'friend';//数据库表名
const PAGE_COUNT =20;//每页20条
public function __construct(){
parent::__construct();
}
public function pageList($page) {
$db = $this->getAdapter();
$sql = "select * from " . $this->_name ;
$pageImpl = new PageImpl($sql, $page, self::PAGE_COUNT);
return $pageImpl->queryResult($db);
}
}
3.调用分页方法FriendController.php
class FriendController extends ActionImpl{
public function init() {
parent::init();
}
public function indexAction() {
$page = (!isset($this->params['p']) || !intval($this->params['p'])) ? 1 : intval($this->params['p']);
$objFriend = new Friend();
$return = $objFriend->pageList($page);
$list = $return[0];
$pages = $return[1];
$pages['link'] = "/friend/";
$this->smarty->assign('result', $list);
$this->smarty->assign('page',$pages);
}
}
4.显示在页面上index.tpl
{foreach from=$result item=value key=key}
<tr>
<td align="center" width="241">{$value->name}</td >
<td align="center" width="425">{$value->url}</td >
<td align="center" width="127">[<a href="/friend/update/?id={$value->id}">修改</a>]</td >
<td width="117" asign="center">[<a href="/friend/del/?id={$value->id}" οnclick="return delpic();">删除</a>]</td >
</tr>
{/foreach}
</table>
<div style="text-align:center;font-size:12px;">
共({$page.total_record})条 {$page.total_page}页
<a href="{$page.link}?p={$page.pre_page}">上一页</a>
{foreach from=$page.page_range item="r"}
{if $r == $page.page}[{$r}]{else}<a href="{$page.link}?p={$r}">{$r}</a>{/if}
{/foreach}
<a href="{$page.link}?p={$page.next_page}">下一页</a>
<input type="text" style="width:30px;" name="page"/>
<input type="button" value="GO"/>
</div>
.