首页 技术 正文
技术 2022年11月16日
0 收藏 305 点赞 3,313 浏览 2944 个字

MVPR下的PHP分页教程

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

MVPR下的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="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <div class="gobalPostImgHeaderCentered" style="background-image: url(\''.$post['abstractImg'].'\');" ></div> </a>
<div class="gobalPostImgTag"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'.$post['categoryName'].'</a> </div>
</div> <!-- gobalPostImg --> <div class="gobalPostContext">
<div class="fontSize20 color111">
<a href="../post/post.php?postId='.$post['id'].'" rel="external nofollow" >'.$post['title'].'</a>
</div>
<div class="mTop2 fontSize12 color666">
'.$post['date'].'
&nbsp 作者:<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'.$post['authorName'].'</a>
&nbsp 编辑:<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'.$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.'" rel="external nofollow" >'.$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分页功能就完成了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289