这个PHP分页其实不难,现在就开始看看核心思路吧。

我习惯从最底层开始看起。

1. 首先用LIMIT偏移QUERY的指针

    /*
* get hot post by current page
* @param [int]startNumOfPPost, [int]numOfPostPerPage
* @return [array]post
*/
function getHotPostByCurrectPage($startNumOfPPost, $numOfPostPerPage)
{
consoleLog('CPost func getHotPostByCurrectPage');
$sql = " SELECT id, category, date, authorId, editorId, title, abstract, abstractImg
FROM post
WHERE visitable = '1' AND isTop = '1'
ORDER BY date DESC, id DESC
LIMIT ".$startNumOfPPost.", ".$numOfPostPerPage;
$result = mysql_query($sql);
while ($post = mysql_fetch_array($result))
{
$post['authorName'] = $this->getNameByUserId($post['authorId']);
$post['editorName'] = $this->getNameByUserId($post['editorId']);
$post['categoryName'] = $this->getCategoryNameById($post['category']);
$arrPost[] = $post;
}
return $arrPost;
}

2. Presenter渲染HTML

    function showHotPostList($currentPage)
{
consoleLogWithTitle('PostPresenter func showHotPostList var currentPage', $currentPage);
$postModel = $this->postModel; // get the first post id of current page
$numOfPostPerPage = $this->numOfPostPerPage;
$startNumOfPPost = ($currentPage-1)*$numOfPostPerPage; // get post from db
$arrPost = $postModel->getHotPostByCurrectPage($startNumOfPPost, $numOfPostPerPage); foreach ($arrPost AS $post)
{
echo '
<div class="gobalPost">
<div class="gobalPostImg">
<a href="#"> <div class="gobalPostImgHeaderCentered" style="background-image: url(\''.$post['abstractImg'].'\');" ></div> </a>
<div class="gobalPostImgTag"> <a href="#">'.$post['categoryName'].'</a> </div>
</div> <!-- gobalPostImg --> <div class="gobalPostContext">
<div class="fontSize20 color111">
<a href="../post/post.php?postId='.$post['id'].'">'.$post['title'].'</a>
</div>
<div class="mTop2 fontSize12 color666">
'.$post['date'].'
&nbsp 作者:<a href="#">'.$post['authorName'].'</a>
&nbsp 编辑:<a href="#">'.$post['editorName'].'</a>
</div>
<div class="mTop8 fontSize13 color333">
'.$post['abstract'].'
</div>
</div> <!-- gobalPostContext -->
</div> <!-- gobalPost -->
';
}
}

3. View,视图层

<?php
$postPresenter->showHotPostList($currentPage);
?>

有了核心的Business层,那就是创建PageSelector。

1. get amoumt of total post in db

2. prensenter render page selector

    function getPageSelectorCellHTML($numOfPage)
{
$selectorCellHTML = "";
for ( $x = 1; $x <= $numOfPage; $x++)
{
$selectorCellHTML .= ' <a class="item" href="../../view/home/index.php?currentPage='.$x.'">'.$x.'</a> ';
}
return $selectorCellHTML;
}
    function renderPageSelector($currentPage)
{
consoleLogWithTitle('PostPresenter func renderPageSelector var currentPage', $currentPage);
$postModel = $this->postModel;
$totalPost = count($postModel->getHotPostOrderById());
$numOfPage = ceil($totalPost / $this->numOfPostPerPage); echo '
<div class="ui basic very padded center aligned segment container">
<div class="ui inverted pagination menu">
<a class="item"><i class="icon purple left arrow"></i></a>
'.$this->getPageSelectorCellHTML($numOfPage).'
<a class="item"><i class="icon purple right arrow"></i></a>
</div>
</div>
';
}

这里要有良好的命名。renderPageSelector是渲染,getPageSelectorCellHTML是构造HTML。

而renderPageSelector是调用getPageSelectorCellHTML,因此要把构造HTML方法,放在主体之前,这是C++的开发概念。

3. View

<?php $postPresenter->renderPageSelector($currentPage); ?>

那PHP分页功能就完成了。

MVPR下的PHP分页教程的更多相关文章

  1. PHP 框架: CodeIgniter 分页教程

    PHP 框架: CodeIgniter 分页教程 分类: PHP2009-04-23 11:09 3209人阅读 评论(0) 收藏 举报 框架phpbooksdatabaseurl数据库 目录(?)[ ...

  2. 10个优秀的 HTML5 & CSS3 下拉菜单制作教程

    下拉菜单是一个很常见的效果,在网站设计中被广泛使用.通过使用下拉菜单,设计者不仅可以在网站设计中营造出色的视觉吸引力,但也可以为网站提供了一个有效的导航方案.使用 HTML5 和 CSS3 可以更容易 ...

  3. linux 下安装apache 快速教程

    最近自学linux,看鸟哥的文章.提到了apache,所以在虚拟机redhat 5下安装了一把, 结合国内外文章写下快速可行的教程: --------------------------------- ...

  4. Windows 下安装 Oracle 12c 教程

    原文 Windows 下安装 Oracle 12c 教程 申明:本文原作者:Jmq   本文给大家带来的是 Oracle 12C 的安装教程. 1.准备 1.1 下载 Oracle 12c 安装程序 ...

  5. CentOS下Eclipse的安装教程

    CentOS下Eclipse的安装教程 据了解,在Linux下的Java开发很多时候都比较喜欢使用vim + 插件,反而很少使用Eclipse,但是我是第一次使用Linux来进行Java编程,就什么都 ...

  6. 简单说下 Winform 的分页快速开发框架必须要实现的几个功能之一

    简单说下 Winform 的分页快速开发框架必须要实现的几个功能之一 分页非为前端分页  和 后端分页,前端分页只有适用于B/S,B/S的呈现速度远远不如C/S,而C/S则没有这个问题,所以分页必然是 ...

  7. 分布式进阶(二)Ubuntu 14.04下安装Dockr图文教程(一)

    当前,完全硬件虚拟化技术(KVM.Xen.Hyper-V 等)能在一个物理主机上很好地运行多个互相独立的操作系统,但这也带来一些问题:性能不佳,资源浪费,系统反应迟缓等.有时候对用户来说,完全的硬件虚 ...

  8. Ubuntu下搜狗输入法的安装教程

            前面写过一篇centos7下搜狗输入法的安装教程,现在把搜狗输入法在Ubuntu下的安装方法也记录一下,相比之下Ubuntu下安装搜狗输入法要简便得多 安装fcitx以支持搜狗输入法 ...

  9. Ubuntu下安装JDK详细教程

    Ubuntu下安装JDK详细教程 作者:凯鲁嘎吉 - 博客园http://www.cnblogs.com/kailugaji/ Ubuntu版本:Ubuntu-12.04.5-desktop-i386 ...

随机推荐

  1. 132-PHP子类和父类同名函数的调用

    <?php class father{ //定义father类 public function cook(){ return '烹饪'; } } class son extends father ...

  2. sendgrid 批量发送邮件,收件栏只显示当前用户的方案

    需求:批量发送邮件,用户可能看到其他用户的邮箱地址,之前用BBC发送,但问题是接收地址是同一个. 官方解决方案:https://sendgrid.kke.co.jp/docs/Tutorials/A_ ...

  3. css mix-blend-mode 颜色滤镜混合模式

    CSS3混合模式种类 在CSS3混合模式中,目前仅有16种:normal,multiply,screen,overlay,darken,lighten,color-dodge,color-burn,h ...

  4. SciKit-Learn 教程

    本教程力求做到简单易懂.深入浅出,帮助你快速掌握机器学习通用库 SciKit-Learn. 机器学习是计算机科学的一个分支,研究的是无需人类干预,能够自己学习的算法. 与TensorFlow不同,Sc ...

  5. Docker 和虚拟机的区别

    版权所有,未经许可,禁止转载 章节 Docker 介绍 Docker 和虚拟机的区别 Docker 安装 Docker Hub Docker 镜像(image) Docker 容器(container ...

  6. JavaScript——装饰者模式

    今天打算开始系统的学习设计模式,虽然之前有看过<大话设计模式>但是没能够静下心来写学习笔记导致很多内容都只是有一个概念而不会去应用.这次要记下学习的过程.接下来进入主题. 何为设计模式?设 ...

  7. mfc 选择文件(可多选)

    CFileDialog dlg( TRUE, NULL, NULL, OFN_ALLOWMULTISELECT, NULL, NULL ); DWORD MAXFILE = ; //2562 is t ...

  8. 洛谷 P1435 回文字串

    题目传送门 解题思路: 就是求一个字符串的最长回文子序列的长度,然后用整个的长度减去最长回文子序列的长度 AC代码: #include<iostream> #include<cstd ...

  9. Vue Element-ui自定义dialog样式

    第一步:定义 自定义dialog class名 第二步:全局修改自定义样式 自定义dialogStyle有三个儿子,这样只用找到他们就可以自定更改啦 .custonStyle { xxxxx } .c ...

  10. Mobile-H5网页快速滚动和回弹

    现在很多for Mobile的HTML5网页内都有快速滚动和回弹的效果,看上去和原生app的效率都有得一拼. 要实现这个效果很简单,只需要加一行css代码即可: -webkit-overflow-sc ...