在这里和大家分享一个非常好用的 Zend Framework 分页类
具体效果可见本站的分页效果, CSS样式可根据个人设计感进行更变。
这里我会举例演示如何使用该类, 如下:
IndexController.php, 在 Action 中写入如下代码:
复制代码 代码如下:
protected $_curPage = 1; //默认第一页
const PERPAGENUM = 4; //每页显示条目数
public function indexAction()
{
// $this->_blogModel 已实例化 blog Model
// $rows -> 获得到所展示数据的总条目数
$rows = $this->_blogModel->getTotalRows();
if($pageNum = $this->getRequest()->getParam('page')) {
//如果有值传入,覆盖初始的第一页
$this->_curPage = $pageNum;
}
//把数据表中的数据传到前端
$this->view->blogInfo = $this->_blogModel->getBlogInfo(
self::PERPAGENUM, ($this->_curPage-1)*self::PERPAGENUM
);
//实例化分页类,并传到前端
$this->view->pagebar = $this->displayPageBar($rows);
}
private function displayPageBar($totalRows)
{
$Pager = new Zend_Pagination($totalRows,self::PERPAGENUM);
return $Pager->getNavigation();
}
models/Blog.php,写入如下代码:
复制代码 代码如下:
public function getBlogInfo($perPageNum = NULL, $limit = NULL)
{
return $this->fetchAll('1 = 1', 'blog_id desc', $perPageNum, $limit)
->toArray();
}
public function getTotalRows($where = '1=1')
{
return $this->fetchAll($where)->count();
}
index.phtml, 写入如下代码:
复制代码 代码如下:
<div class="page">
<!--"codetitle">复制代码 代码如下:
class Zend_Pagination
{
private $_navigationItemCount = 6; //导航栏显示导航总页数
private $_pageSize = null; //每页项目数
private $_align = "right"; //导航栏显示位置
private $_itemCount = null; //总项目数
private $_pageCount = null; //总页数
private $_currentPage = null; //当前页
private $_front = null; //前端控制器
private $_PageParaName = "page"; //页面参数名称
private $_firstPageString = "|<<"; //导航栏中第一页显示的字符
private $_nextPageString = ""; //导航栏中前一页显示的字符
private $_previousPageString = "<<"; //导航栏中后一页显示的字符
private $_lastPageString = "|"; //导航栏中最后一页显示的字符
private $_splitString = " | "; //页数字间的间隔符
public function __construct($itemCount, $pageSize)
{
if (!is_numeric($itemCount) || (!is_numeric($pageSize))) {
throw new Exception("Pagination Error:not Number");
}
$this->_itemCount = $itemCount;
$this->_pageSize = $pageSize;
$this->_front = Zend_Controller_Front::getInstance();
$this->_pageCount = ceil($itemCount/$pageSize); //总页数
$page = $this->_front->getRequest()->getParam($this->_PageParaName);
if (empty($page) || (!is_numeric($page))) {
//为空或不是数字,设置当前页为1
$this->_currentPage = 1;
} else {
if ($page < 1) {
$page = 1;
}
if ($page > $this->_pageCount) {
$page = $this->_pageCount;
}
$this->_currentPage = $page;
}
}
public function getCurrentPage()
{
return $this->_currentPage;
}
public function getNavigation()
{
$navigation = '<div style="text-align:'.$this->_align.';" class="pagecss">';
//当前页处于第几栏分页
$pageCote = ceil($this->_currentPage / ($this->_navigationItemCount - 1)) - 1;
//总分页栏
$pageCoteCount = ceil($this->_pageCount / ($this->_navigationItemCount - 1));
//分页栏中起始页
$pageStart = $pageCote * ($this->_navigationItemCount -1) + 1;
//分页栏中终止页
$pageEnd = $pageStart + $this->_navigationItemCount - 1;
if($this->_pageCount < $pageEnd) {
$pageEnd = $this->_pageCount;
}
$navigation .= "总共: {$this->_itemCount} 条 共 {$this->_pageCount} 页\n ";
if($pageCote > 0) { //首页导航
$navigation .= '<a href="'.$this->createHref(1)
." \"="">$this->_firstPageString</a> ";
}
if($this->_currentPage != 1) { //上一页导航
$navigation .= '<a href="'.$this->createHref($this->_currentPage-1);
$navigation .= " \"="">$this->_previousPageString</a> ";
}else{
$navigation .= $this->_previousPageString . ' ';
}
while ($pageStart <= $pageEnd) //构造数字导航区
{
if ($pageStart == $this->_currentPage) {
$navigation .= "<b>$pageStart</b>" . $this->_splitString;
} else {
$navigation .= '<a href="'.$this->createHref($pageStart)
." \"="">$pageStart</a>"
. $this->_splitString;
}
$pageStart++;
}
if($this->_currentPage != $this->_pageCount) { //下一页导航
$navigation .= ' <a href="'
. $this->createHref($this->_currentPage+1)
. " \"="">$this->_nextPageString</a> ";
}else{
$navigation .= $this->_nextPageString;
}
if ($pageCote < $pageCoteCount-1) { //未页导航
$navigation .= '<a href="'
. $this->createHref($this->_pageCount)
. " \"="">$this->_lastPageString</a> ";
}
$navigation .= ' 到 <select onchange="window.location=\' '
. $this->createHref()
. '\'+this.options[this.selectedIndex].value;">';
for ($i=1;$i<=$this->_pageCount;$i++){
if ($this->getCurrentPage()==$i){
$selected = "selected";
} else {
$selected = "";
}
$navigation .= '<option value=" . $i . " '="" .="" $selected="">'
. $i
. '</option>';
}
$navigation .= '</select>';
$navigation .= " 页</div>";
return $navigation;
}
public function getNavigationItemCount()
{
return $this->_navigationItemCount;
}
public function setNavigationItemCoun($navigationCount)
{
if(is_numeric($navigationCount)) {
$this->_navigationItemCount = $navigationCount;
}
}
public function setFirstPageString($firstPageString)
{
$this->_firstPageString = $firstPageString;
}
public function setPreviousPageString($previousPageString)
{
$this->_previousPageString = $previousPageString;
}
public function setNextPageString($nextPageString)
{
$this->_nextPageString = $nextPageString;
}
public function setLastPageString($lastPageString)
{
$this->_lastPageString = $lastPageString;
}
public function setAlign($align)
{
$align = strtolower($align);
if ($align == "center") {
$this->_align = "center";
} elseif ($align == "right") {
$this->_align = "right";
} else {
$this->_align = "left";
}
}
public function setPageParamName($pageParamName)
{
$this->_PageParaName = $pageParamName;
}
public function getPageParamName()
{
return $this->_PageParaName;
}
private function createHref($targetPage = null)
{
$params = $this->_front->getRequest()->getParams();
$module = $params["module"];
$controller = $params["controller"];
$action = $params["action"];
$targetUrl = $this->_front->getBaseUrl()
. "/$module/$controller/$action";
foreach ($params as $key => $value)
{
if($key != "controller" && $key != "module"
&& $key != "action" && $key != $this->_PageParaName) {
$targetUrl .= "/$key/$value";
}
}
if (isset($targetPage)) { //指定目标页
$targetUrl .= "/$this->_PageParaName/$targetPage";
} else {
$targetUrl .= "/$this->_PageParaName/";
}
return $targetUrl;
}
}
这里再简单回顾下 Mysql 中的 limit offset
假设数据库表 blog 存在 13 条数据。
语句1:select * from blog limit 9, 4
语句2:select * from blog limit 4 offset 9
//语句1和2均返回表 blog 的第 10、11、12、13 行
//语句1中的 9 表示从表的第十行开始, 返回 4 行
//语句2中的 4 表示返回 4 行,offset 9 表示从表的第十行开始
如下语句显示分页效果:
语句3:select * from blog limit ($this->_curPage - 1)* self::PERPAGENUM, self::PERPAGENUM;
语句4:select * from blog limit self::PERPAGENUM offset ($this->_curPage - 1) * self::PERPAGENUM;
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]