PHP分页函数(PHP代码+JS+CSS完整实例)_PHP教程
教程Tag:暂无Tag,欢迎添加,赚取U币!
推荐:php环境搭建:PHPnow绿色PHP环境包很多新手朋友,想学习建站,使用现在比较热门的基于PHP开发的CMS系统,碰到的第一个问题就是如何搭建PHP环境. 网上有很多PHP集成环境的程序,模板无忧会在近期一一为大家讲解常见的PHP环境搭建程序的安装和使用方法. 今天为大家介绍一款绿色的PHP环境包,也是站长本人一直在
PHP分页函数(PHP代码+JS+CSS完整实例)PHP分页器制作,自动生成分面页码,JS调用函数.
PHP代码如下:
CSS代码如下:
- <?php
- class PageView{
- /**页码**/
- public $pageNo = 1;
- /**页大小**/
- public $pageSize = 20;
- /**共多少页**/
- public $pageCount = 0;
- /**总记录数**/
- public $totalNum = 0;
- /**偏移量,当前页起始行**/
- public $offSet = 0;
- /**每页数据**/
- public $pageData = array();
- /**是否有上一页**/
- public $hasPrePage = true;
- /**是否有下一页**/
- public $hasNextPage = true;
- public $pageNoList = array();
- public $jsFunction ='jsFunction';
- /**
- *
- * @param unknown_type $count 总行数
- * @param unknown_type $size 分页大小
- * @param unknown_type $string
- */
- public function __construct($count=0, $size=20,$pageNo=1,$pageData =array(),$jsFunction='jsFunction'){
- $this->totalNum = $count;//总记录数
- $this->pageSize = $size;//每页大小
- $this->pageNo = $pageNo;
- //计算总页数
- $this->pageCount = ceil($this->totalNum/$this->pageSize);
- $this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;
- //检查pageNo
- $this->pageNo = $this->pageNo == 0 ? 1 : $this->pageNo;
- $this->pageNo = $this->pageNo > $this->pageCount? $this->pageCount : $this->pageNo;
- //计算偏移
- $this->offset = ( $this->pageNo - 1 ) * $this->pageSize;
- //计算是否有上一页下一页
- $this->hasPrePage = $this->pageNo == 1 ?false:true;
- $this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true;
- $this->pageData = $pageData;
- $this->jsFunction = $jsFunction;
- }
- /**
- * 分页算法
- * @return
- */
- private function generatePageList(){
- $pageList = array();
- if($this->pageCount <= 9){
- for($i=0;$i<$this->pageCount;$i++){
- array_push($pageList,$i+1);
- }
- }else{
- if($this->pageNo <= 4){
- for($i=0;$i<5;$i++){
- array_push($pageList,$i+1);
- }
- array_push($pageList,-1);
- array_push($pageList,$this->pageCount);
- }else if($this->pageNo > $this->pageCount - 4){
- array_push($pageList,1);
- array_push($pageList,-1);
- for($i=5;$i>0;$i--){
- array_push($pageList,$this->pageCount - $i+1);
- }
- }else if($this->pageNo > 4 && $this->pageNo <= $this->pageCount - 4){
- array_push($pageList,1);
- array_push($pageList,-1);
- array_push($pageList,$this->pageNo -2);
- array_push($pageList,$this->pageNo -1);
- array_push($pageList,$this->pageNo);
- array_push($pageList,$this->pageNo + 1);
- array_push($pageList,$this->pageNo + 2);
- array_push($pageList,-1);
- array_push($pageList,$this->pageCount);
- }
- }
- return $pageList;
- }
- /***
- * 创建分页控件
- * @param
- * @return String
- */
- public function echoPageAsDiv(){
- $pageList = $this->generatePageList();
- $pageString ="<div class='pagination'><div class='page-bottom'>";
- if(!emptyempty($pageList)){
- if($this->pageCount >1){
- if($this->hasPrePage){
- $pageString = $pageString ."<a class='page-next' href=\"javascript:" .$this->jsFunction . "(" . ($this->pageNo-1) . ")\">上一页</a>";
- }
- foreach ($pageList as $k=>$p){
- if($this->pageNo == $p){
- $pageString = $pageString ."<span class='page-cur'>" . $this->pageNo . "</span>";
- continue;
- }
- if($p == -1){
- $pageString = $pageString ."<span class='page-break'>...</span>";
- continue;
- }
- $pageString = $pageString ."<a href=\"javascript:" .$this->jsFunction . "(" . $p . ")\">" . $p . "</a>";
- }
- if($this->hasNextPage){
- $pageString = $pageString ."<a class='page-next' href=\"javascript:" .$this->jsFunction . "(" . ($this->pageNo+1) . ")\">下一页</a>";
- }
- }
- }
- $pageString = $pageString .("</div></div>");
- return $pageString;
- }
- }
- ?>
PHP调用代码:
- <style type="text/css">
- <!--
- .pagination {font-family: Tahoma;overflow: hidden; padding-top: 12px; text-align: center;}
- .pagination-tab { margin-bottom: 20px;}
- .pagination a, .pagination .page-cur, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-break, .pagination .page-skip {
- display: inline-block;font-family: Tahoma,SimSun,Arial; height: 22px;line-height:22px; margin: 0; min-width: 16px;padding: 0 5px; text-align: center; vertical-align: top; white-space: nowrap;}
- .pagination a, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .page-break {
- border: 1px solid #ed3d83; color:#e9357d; font-weight:bold;}
- .pagination a:hover { border: 1px solid #ed3d83;text-decoration: none; background-color:#f95f9d; color:#fff;}
- .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g { width: 36px; background-image: url(../static/img/page.gif);}
- .pagination .page-prev { background-position: -0px -38px; padding-left: 16px;}
- .pagination .page-prev_g { background-position:0px -59px; padding-left: 16px; color:#cbcbcb; font-weight:normal;}
- .pagination .page-next { background-position: 0px 0px; padding-right: 16px; font-weight:normal;}
- .pagination .page-next_g { background-position: -0px -19px; padding-right: 16px; color:#cbcbcb;}
- .pagination .page-cur {background-color: #f95f9d; border: 1px solid #ed3d83;color: #fff;font-weight: bold;}
- .pagination .page-break {border: medium none; background:none transparent; color:#333;}
- -->
- </style>
效果图如下:
- $pageNo = $_GET['pageNo'];
- if(emptyempty($pageNo)){
- $pageNo = 1;
- }
- //分页数据
- $pageData = News::getNewsPage($pageNo,$pageSize);
- //取得总行数
- $count = News::getNewsCount();
- //创建分页器
- $p = new PageView($count['0']['TOTAL'],$pageSize,$pageNo,$pageData);
- //生成页码
- $pageViewString = $p->echoPageAsDiv();
分享:PHP面向对象—单例模式单例模式是设计模式中比较简单的模式,会在一些特殊的场影得以运用,比如:应用中只需要一个对象实例存在,这时使用单例模式能够节省内存。单例模式体现了面向对象封装特性中的单一职责和对象自治原则。 构建一个PHP的单例类需要以下要求: 构造函数为private,防止外
相关PHP教程:
- 相关链接:
- 教程说明:
PHP教程-PHP分页函数(PHP代码+JS+CSS完整实例)。