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

一个分页导航类

?php//+----------------------------------------------------------------------+//|PHPVersion4|//+----------------------------------------------------------------
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Richard Heyes                          |
// +----------------------------------------------------------------------+

/**
* Pager class
*
* Handles paging a set of data. For usage see the example.php provided.
*
*/

class Pager {

    /**
    * Current page
    * @var integer
    */
    var $_currentPage;

    /**
    * Items per page
    * @var integer
    */
    var $_perPage;

    /**
    * Total number of pages
    * @var integer
    */
    var $_totalPages;

    /**
    * Item data. Numerically indexed array...
    * @var array
    */
    var $_itemData;

    /**
    * Total number of items in the data
    * @var integer
    */
    var $_totalItems;

    /**
    * Page data generated by this class
    * @var array
    */
    var $_pageData;

    /**
    * Constructor
    *
    * Sets up the object and calculates the total number of items.
    *
    * @param $params An associative array of parameters This can contain:
    *                  currentPage   Current Page number (optional)
    *                  perPage       Items per page (optional)
    *                  itemData      Data to page
    */
    function pager($params = array())
    {
        global $HTTP_GET_VARS;

        $this->_currentPage = max((int)@$HTTP_GET_VARS['pageID'], 1);
        $this->_perPage     = 8;
        $this->_itemData    = array();

        foreach ($params as $name => $value) {
            $this->{'_' . $name} = $value;
        }

        $this->_totalItems = count($this->_itemData);
    }

    /**
    * Returns an array of current pages data
    *
    * @param $pageID Desired page ID (optional)
    * @return array Page data
    */
    function getPageData($pageID = null)
    {
        if (isset($pageID)) {
            if (!empty($this->_pageData[$pageID])) {
                return $this->_pageData[$pageID];
            } else {
                return FALSE;
            }
        }

        if (!isset($this->_pageData)) {
            $this->_generatePageData();
        }

        return $this->getPageData($this->_currentPage);
    }

    /**
    * Returns pageID for given offset
    *
    * @param $index Offset to get pageID for
    * @return int PageID for given offset
    */
    function getPageIdByOffset($index)
    {
        if (!isset($this->_pageData)) {
            $this->_generatePageData();
        }

        if (($index % $this->_perPage) > 0) {
            $pageID = ceil((float)$index / (float)$this->_perPage);
        } else {
            $pageID = $index / $this->_perPage;
        }

        return $pageID;
    }

    /**
    * Returns offsets for given pageID. Eg, if you
    * pass it pageID one and your perPage limit is 10
    * it will return you 1 and 10. PageID of 2 would
    * give you 11 and 20.
    *
    * @params pageID PageID to get offsets for
    * @return array  First and last offsets
    */
    function getOffsetByPageId($pageid = null)
    {
        $pageid = isset($pageid) ? $pageid : $this->_currentPage;
        if (!isset($this->_pageData)) {
            $this->_generatePageData();
        }

        if (isset($this->_pageData[$pageid])) {
            return array(($this->_perPage * ($pageid - 1)) + 1, min($this->_totalItems, $this->_perPage * $pageid));
        } else {
            return array(0,0);
        }
    }

    /**
    * Returns back/next and page links
    *
    * @param  $back_html HTML to put inside the back link
    * @param  $next_html HTML to put inside the next link
    * @return array Back/pages/next links
    */
    function getLinks($back_html = '<     {
        $url   = $this->_getLinksUrl();
        $back  = $this->_getBackLink($url, $back_html);
        $pages = $this->_getPageLinks($url);
        $next  = $this->_getNextLink($url, $next_html);

        return array($back, $pages, $next, 'back' => $back, 'pages' => $pages, 'next' => $next);
    }

    /**
    * Returns number of pages
    *
    * @return int Number of pages
    */
    function numPages()
    {
        return $this->_totalPages;
    }

    /**
    * Returns whether current page is first page
    *
    * @return bool First page or not
    */
    function isFirstPage()
    {
        return ($this->_currentPage == 1);
    }

    /**
    * Returns whether current page is last page
    *
    * @return bool Last page or not
    */
    function isLastPage()
    {
        return ($this->_currentPage == $this->_totalPages);
    }

    /**
    * Returns whether last page is complete
    *
    * @return bool Last age complete or not
    */
    function isLastPageComplete()
    {
        return !($this->_totalItems % $this->_perPage);
    }

    /**
    * Calculates all page data
    */
    function _generatePageData()
    {
        $this->_totalItems = count($this->_itemData);
        $this->_totalPages = ceil((float)$this->_totalItems / (float)$this->_perPage);
        $i = 1;
        if (!empty($this->_itemData)) {
            foreach ($this->_itemData as $value) {
                $this->_pageData[$i][] = $value;
                if (count($this->_pageData[$i]) >= $this->_perPage) {
                    $i++;
                }
            }
        } else {
            $this->_pageData = array();
        }
    }

    /**
    * Returns the correct link for the back/pages/next links
    *
    * @return string Url
    */
    function _getLinksUrl()
    {
        global $HTTP_SERVER_VARS;

        // Sort out query string to prevent messy urls
        $querystring = array();
        if (!empty($HTTP_SERVER_VARS['QUERY_STRING'])) {
            $qs = explode('&', $HTTP_SERVER_VARS['QUERY_STRING']);            
            for ($i = 0, $cnt = count($qs); $i <$cnt; $i++) {
                list($name, $value) = explode('=', $qs[$i]);
                if ($name != 'pageID') {
                    $qs[$name] = $value;
                }
                unset($qs[$i]);
            }
        }
    if(is_array($qs)){
            foreach ($qs as $name => $value) {
                $querystring[] = $name . '=' . $value;
            }    
    }
        return $HTTP_SERVER_VARS['SCRIPT_NAME'] . '?' . implode('&', $querystring) . (!empty($querystring) ? '&' : ') . 'pageID=';
    }

    /**
    * Returns back link
    *
    * @param $url  URL to use in the link
    * @param $link HTML to use as the link
    * @return string The link
    */
    function _getBackLink($url, $link = '<     {
        // Back link
        if ($this->_currentPage > 1) {
            $back = '' . $link . '';
        } else {
            $back = ';
        }
        
        return $back;
    }

    /**
    * Returns pages link
    *
    * @param $url  URL to use in the link
    * @return string Links
    */
    function _getPageLinks($url)
    {
        // Create the range
        $params['itemData'] = range(1, max(1, $this->_totalPages));
        $pager =& new Pager($params);
        $links =  $pager->getPageData($pager->getPageIdByOffset($this->_currentPage));

        for ($i=0; $i             if ($links[$i] != $this->_currentPage) {
                $links[$i] = '' . $links[$i] . '';
            }
        }

        return implode(' ', $links);
    }

    /**
    * Returns next link
    *
    * @param $url  URL to use in the link
    * @param $link HTML to use as the link
    * @return string The link
    */
    function _getNextLink($url, $link = 'Next >>')
    {
        if ($this->_currentPage <$this->_totalPages) {
            $next = '' . $link . '';
        } else {
            $next = ';
        }

        return $next;
    }
}

?>
推荐阅读
  • 如何在Faceu激萌中设置和使用妆容切换特效?
    本文将详细介绍如何在Faceu激萌应用中设置和使用妆容切换特效,帮助用户轻松实现创意拍摄。无论是新手还是有经验的用户,都能从中受益。 ... [详细]
  • 本文介绍了拍摄高质量Vlog所需的设备,包括索尼A7 III相机、蔡司镜头、罗德麦克风、单反稳定器、苹果手机及其配件、灯光设备等。此外,还探讨了后期制作所需的软件工具,如剪辑、特效和调色软件。无论你是业余爱好者还是专业创作者,选择合适的设备至关重要。 ... [详细]
  • Python 异步编程:深入理解 asyncio 库(上)
    本文介绍了 Python 3.4 版本引入的标准库 asyncio,该库为异步 IO 提供了强大的支持。我们将探讨为什么需要 asyncio,以及它如何简化并发编程的复杂性,并详细介绍其核心概念和使用方法。 ... [详细]
  • 探讨一个老旧 PHP MySQL 系统中,时间戳字段不定期出现异常值的问题及其可能原因。 ... [详细]
  • 国内BI工具迎战国际巨头Tableau,稳步崛起
    尽管商业智能(BI)工具在中国的普及程度尚不及国际市场,但近年来,随着本土企业的持续创新和市场推广,国内主流BI工具正逐渐崭露头角。面对国际品牌如Tableau的强大竞争,国内BI工具通过不断优化产品和技术,赢得了越来越多用户的认可。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 郑州大学在211高校中的地位与排名解析
    本文将详细解读郑州大学作为一所位于河南省的211和双一流B类高校,在全国211高校中的地位与排名,帮助高三学生更好地了解这所知名学府的实力与发展前景。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 优化ASM字节码操作:简化类转换与移除冗余指令
    本文探讨如何利用ASM框架进行字节码操作,以优化现有类的转换过程,简化复杂的转换逻辑,并移除不必要的加0操作。通过这些技术手段,可以显著提升代码性能和可维护性。 ... [详细]
  • 本文总结了2018年的关键成就,包括职业变动、购车、考取驾照等重要事件,并分享了读书、工作、家庭和朋友方面的感悟。同时,展望2019年,制定了健康、软实力提升和技术学习的具体目标。 ... [详细]
  • 电子元件封装库:三极管、MOS管及部分LDO(含3D模型)
    本资源汇集了常用的插件和贴片三极管、MOS管以及部分LDO的封装,涵盖TO和SOT系列。所有封装均配有高质量的3D模型,共计96种,满足日常设计需求。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • 本文详细介绍了如何使用PHP检测AJAX请求,通过分析预定义服务器变量来判断请求是否来自XMLHttpRequest。此方法简单实用,适用于各种Web开发场景。 ... [详细]
  • 小红书提高MCN机构入驻门槛,需缴纳20万元保证金
    近期,小红书对MCN机构的入驻要求进行了调整,明确要求MCN机构在入驻时需缴纳20万元人民币的保证金。此举旨在进一步规范平台内容生态,确保社区的真实性和用户体验。 ... [详细]
author-avatar
芬妮诗婚纱厂
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有