热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP中常用的分页类总结

分页是目前在显示大量结果时所采用的最好的方式。有了下面这些代码的帮助,开发人员可以在多个页面中显示大量的数据。在互联网上,分页是一般用于搜索结果或是浏览全部信息php基本分页&l

分页是目前在显示大量结果时所采用的最好的方式。有了下面这些代码的帮助,开发人员可以在多个页面中显示大量的数据。在互联网上,分页是一般用于搜索结果或是浏览全部信息

php基本分页

 $totalpages) {
    // set current page to last page
    $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
    // set current page to first page
    $currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
    // echo data
    echo $list[&#39;id&#39;] . " : " . $list[&#39;number&#39;] . "";
} // end while
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don&#39;t show back links
if ($currentpage > 1) {
    // show << link to go back to page 1
    echo " << ";
    // get previous page num
    $prevpage = $currentpage - 1;
    // show < link to go back to 1 page
    echo " < ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
    // if it&#39;s a valid page number...
    if (($x > 0) && ($x <= $totalpages)) {
        // if we&#39;re on current page...
        if ($x == $currentpage) {
            // &#39;highlight&#39; it but don&#39;t make a link
            echo " [$x] ";
            // if not current page...
            
        } else {
            // make it a link
            echo " $x ";
        } // end else
        
    } // end if
    
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
    // get next page
    $nextpage = $currentpage + 1;
    // echo forward link for next page
    echo " > ";
    // echo forward link for lastpage
    echo " >> ";
} // end if
/****** end build pagination links ******/
?>

先看一个常用的php分页类

 1) {
    $pagination.= "";
    //previous button
    if ($page > 1) $pagination.= " previous";
    else $pagination.= " previous";
    //pages
    if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
    {
        for ($counter = 1; $counter <= $lastpage; $counter++) {
            if ($counter == $page) $pagination.= "$counter";
            else $pagination.= "$counter";
        }
    } elseif ($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
    {
        //close to beginning; only hide later pages
        if ($page < 1 + ($adjacents * 2)) {
            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) {
                if ($counter == $page) $pagination.= "$counter";
                else $pagination.= "$counter";
            }
            $pagination.= "...";
            $pagination.= "$lpm1";
            $pagination.= "$lastpage";
        }
        //in middle; hide some front and some back
        elseif ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
            $pagination.= "1";
            $pagination.= "2";
            $pagination.= "...";
            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
                if ($counter == $page) $pagination.= "$counter";
                else $pagination.= "$counter";
            }
            $pagination.= "...";
            $pagination.= "$lpm1";
            $pagination.= "$lastpage";
        }
        //close to end; only hide early pages
        else {
            $pagination.= "1";
            $pagination.= "2";
            $pagination.= "...";
            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
                if ($counter == $page) $pagination.= "$counter";
                else $pagination.= "$counter";
            }
        }
    }
    //next button
    if ($page < $counter - 1) $pagination.= "next ";
    else $pagination.= "next ";
    $pagination.= "
n"; } ?>     实例 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 = "";         if (!empty($pageList)) {             if ($this->pageCount > 1) {                 if ($this->hasPrePage) {                     $pageString = $pageString . "jsFunction . "(" . ($this->pageNo-1) . ") ">上一页";                 }                 foreach ($pageList as $k => $p) {                     if ($this->pageNo == $p) {                         $pageString = $pageString . "" . $this->pageNo . "";                         continue;                     }                     if ($p == - 1) {                         $pageString = $pageString . "...";                         continue;                     }                     $pageString = $pageString . "jsFunction . "(" . $p . ")\">" . $p . "";                 }                 if ($this->hasNextPage) {                     $pageString = $pageString . "jsFunction . "(" . ($this->pageNo + 1) . ")\">下一页";                 }             }         }         $pageString = $pageString . ("
");         return $pageString;     } } ?>

css代码

在php页面中的调用方法

echoPageAsDiv();


随意转载^^但请附上教程地址。


推荐阅读
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 基因组浏览器中的Wig格式解析
    本文详细介绍了Wiggle(Wig)格式及其在基因组浏览器中的应用,涵盖variableStep和fixedStep两种主要格式的特点、适用场景及具体使用方法。同时,还提供了关于数据值和自定义参数的补充信息。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
author-avatar
村头的小路_157
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有