【代码】PHP 分页种
程序员文章站
2024-02-11 12:02:58
...
【代码】PHP 分页类
下面是我自己写的分页类,可做简单的修改
[php]
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 = ""; $this->startRow = $this->post; $this->offset = ($this->post - 1) * $this->pageSize; } for ($i = 1; $i pageTotal;$i++){ if ($i != $this->post) { $pageNums .= ""; } } 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]
本文出自 “清源教育” 博客,转载请注明此处,谢谢!
本文出自 “清源教育” 博客,转载请注明此处,谢谢!
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
- 最新文章
- 热门排行
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论