0

0

一款万能的php分页类实例代码

怪我咯

怪我咯

发布时间:2017-07-23 11:03:56

|

2358人浏览过

|

来源于php中文网

原创

分页 (pagination),即将一个页面分成两个或两个以上的页面。

有一种自动分页机制,可以将移动 Web窗体中的内

容分割成一组组较小的页进行呈现,以适合于特定的设备。该机制还呈现可用于浏览到其他页的用户界面元素。

这篇文章主要介绍了一款万能的php分页类,特别好用,需要使用php分页类的朋友不要错过。代码如下

立即学习PHP免费学习笔记(深入)”;

触网万能商城建站系统免费版
触网万能商城建站系统免费版

触网万能商城系统,3年专注打磨一款产品,专为网络服务公司、建站公司、威客、站长、设计师、网络运营及营销人员打造,是一款超级万能建站利器,彻底告别代码编程和找模板,改模板,改代码的低效高成本方式,仅需一个人可服务无数客户,系统集万能官网+万能商城+万能表单+博客+新闻+分销...于一体,通过海量模块拖拽布局、万能组合和超级自定义功能,可以构建各种类型的响应式网站。

下载

init(1000, 20);
 * $page->setNotActiveTemplate(' {a} ');
 * $page->setActiveTemplate('{a}');
 * echo $page->show();
 * 
 * 
 * @author 风居住的地方
 */
class Page {
  /**
   * 总条数
   */
  private $total;
  /**
   * 每页大小
   */
  private $pageSize;
  /**
   * 总页数
   */
  private $pageNum;
  /**
   * 当前页
   */
  private $page;
  /**
   * 地址
   */
  private $uri;
  /**
   * 分页变量
   */
  private $pageParam;
  /**
   * LIMIT XX,XX
   */
  private $limit;
  /**
   * 数字分页显示
   */
  private $listnum = 8;
  /**
   * 分页显示模板
   * 可用变量参数
   * {total}   总数据条数
   * {pagesize}  每页显示条数
   * {start}   本页开始条数
   * {end}    本页结束条数
   * {pagenum}  共有多少页
   * {frist}   首页
   * {pre}    上一页
   * {next}    下一页
   * {last}    尾页
   * {list}    数字分页
   * {goto}    跳转按钮
   */
  private $template = '

共有{total}条数据每页显示{pagesize}条数据,本页{start}-{end}条数据共有{pagenum}页

    {frist}{pre}{list}{next}{last}{goto}

'; /** * 当前选中的分页链接模板 */ private $activeTemplate = '
  • {text}
  • '; /** * 未选中的分页链接模板 */ private $notActiveTemplate = '
  • {text}
  • '; /** * 显示文本设置 */ private $config = array('frist' => '首页', 'pre' => '上一页', 'next' => '下一页', 'last' => '尾页'); /** * 初始化 * @param type $total 总条数 * @param type $pageSize 每页大小 * @param type $param url附加参数 * @param type $pageParam 分页变量 */ public function init($total, $pageSize, $param = '', $pageParam = 'page') { $this->total = intval($total); $this->pageSize = intval($pageSize); $this->pageParam = $pageParam; $this->uri = $this->geturi($param); $this->pageNum = ceil($this->total / $this->pageSize); $this->page = $this->setPage(); $this->limit = $this->setlimit(); } /** * 设置分页模板 * @param type $template 模板配置 */ public function setTemplate($template) { $this->template = $template; } /** * 设置选中分页模板 * @param type $activeTemplate 模板配置 */ public function setActiveTemplate($activeTemplate) { $this->activeTemplate = $activeTemplate; } /** * 设置未选中分页模板 * @param type $notActiveTemplate 模板配置 */ public function setNotActiveTemplate($notActiveTemplate) { $this->notActiveTemplate = $notActiveTemplate; } /** * 返回分页 * @return type */ public function show() { return str_ireplace(array( '{total}', '{pagesize}', '{start}', '{end}', '{pagenum}', '{frist}', '{pre}', '{next}', '{last}', '{list}', '{goto}', ), array( $this->total, $this->setPageSize(), $this->star(), $this->end(), $this->pageNum, $this->frist(), $this->prev(), $this->next(), $this->last(), $this->pagelist(), $this->gopage(), ), $this->template); } /** * 获取limit起始数 * @return type */ public function getOffset() { return ($this->page - 1) * $this->pageSize; } /** * 设置LIMIT * @return type */ private function setlimit() { return "limit " . ($this->page - 1) * $this->pageSize . ",{$this->pageSize}"; } /** * 获取limit * @param type $args * @return type */ public function __get($args) { if ($args == "limit") { return $this->limit; } else { return null; } } /** * 初始化当前页 * @return int */ private function setPage() { if (!empty($_GET[$this->pageParam])) { if ($_GET[$this->pageParam] > 0) { if ($_GET[$this->pageParam] > $this->pageNum) return $this->pageNum; else return $_GET[$this->pageParam]; } } return 1; } /** * 初始化url * @param type $param * @return string */ private function geturi($param) { $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], "?") ? "" : "?") . $param; $parse = parse_url($url); if (isset($parse["query"])) { parse_str($parse["query"], $params); unset($params["page"]); $url = $parse["path"] . "?" . http_build_query($params); return $url; } else { return $url; } } /** * 本页开始条数 * @return int */ private function star() { if ($this->total == 0) { return 0; } else { return ($this->page - 1) * $this->pageSize + 1; } } /** * 本页结束条数 * @return type */ private function end() { return min($this->page * $this->pageSize, $this->total); } /** * 设置当前页大小 * @return type */ private function setPageSize() { return $this->end() - $this->star() + 1; } /** * 首页 * @return type */ private function frist() { $html = ''; if ($this->page == 1) { $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], true); } else { $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], false); } return $html; } /** * 上一页 * @return type */ private function prev() { $html = ''; if ($this->page > 1) { $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], false); } else { $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], true); } return $html; } /** * 分页数字列表 * @return type */ private function pagelist() { $linkpage = ""; $lastlist = floor($this->listnum / 2); for ($i = $lastlist; $i >= 1; $i--) { $page = $this->page - $i; if ($page >= 1) { $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false); } else { continue; } } $linkpage .= $this->replace("{$this->uri}&page={$this->page}", $this->page, true); for ($i = 1; $i <= $lastlist; $i++) { $page = $this->page + $i; if ($page <= $this->pageNum) { $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false); } else { break; } } return $linkpage; } /** * 下一页 * @return type */ private function next() { $html = ''; if ($this->page < $this->pageNum) { $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], false); } else { $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], true); } return $html; } /** * 最后一页 * @return type */ private function last() { $html = ''; if ($this->page == $this->pageNum) { $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], true); } else { $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], false); } return $html; } /** * 跳转按钮 * @return string */ private function gopage() { $html = ''; $html.=' '; return $html; } /** * 模板替换 * @param type $replace 替换内容 * @param type $result 条件 * @return type */ private function replace($url, $text, $result = true) { $template = ($result ? $this->activeTemplate : $this->notActiveTemplate); $html = str_replace('{url}', $url, $template); $html = str_replace('{text}', $text, $html); return $html; } }

    第二款php分页类

    page_num = ceil( $total / $this->length );
        return $this->page_num;
      }
    
      function Calculation_Min_Max( $act_page = 1 )
      {
        // 定义左右偏移量
        $py_left = 0;
        $py_right = 0;
        // 定义左右边界
        $bj_left = 0;
        $bj_right = 0;
        // 定义滚动区间边界
        $gd_left = 0;
        $gd_right = 0;
        // 判断是否需要分组
        if ( ( $this->page_num - $this->page_max ) <= 0 )
        {
          // 不需要分组
          $bj_left = 1;
          $bj_right = $this->page_num;
        }
        else
        {
          // 要进行分组
          // 判断容量的奇偶
          $tmp = $this->page_max % 2;
          if ( $tmp === 1 )
          {
            // 奇数
            $py_left = $py_right = ( $this->page_max - 1 ) / 2;
          }
          else
          {
            // 偶数
            $py_left = $this->page_max / 2 - 1;
            $py_right = $this->page_max / 2;
          }
          // 计算滚动区间
          $gd_left = 1 + $py_left;
          $gd_right = $this->page_num - $py_right;
          // 判断当前页是否落入了滚动区间
          if ( $act_page >= $gd_left && $act_page <= $gd_right )
          {
            // 区间内
            $bj_left = $act_page - $py_left;
            $bj_right = $act_page + $py_right;
          }
          else
          {
            // 区间外
            if ( ( $act_page - $py_left ) <= 1 )
            {
              // 左侧固定区间
              $bj_left = 1;
              $bj_right = $this->page_max;
            }
            else
            {
              $bj_left = $this->page_num - $this->page_max + 1;
              $bj_right = $this->page_num;
            }
          }
        }
    
        $res = array();
        $res['min'] = $bj_left;
        $res['max'] = $bj_right;
    
        return $res;
        
      }
      // 主方法
      function make_page( $total, $act_page, $url, $param )
      {
        $page_num = $this->Calculation_Page_Num( $total );
        $arr_min_max = $this->Calculation_Min_Max( $act_page );
        
        if (!eregi("([?|&]$param=)", $url)) {
          $url = strpos($url,"?")===false?$url."?":$url."&";
          $url = $url."$param=0";
        }
    
        if ( $act_page > $page_num )
        {
          $act_page = $page_num;
        }
        // 用正则把url改成正规的
        $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url );
    
        $res = array();
        $d = 0;
        for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ )
        {
          if ( $i == $act_page )
          {
            $res[$d]['url'] = '';
            $res[$d]['name'] = $i;
            $res[$d]['no'] = $i;
          }
          else
          {
            $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
            $res[$d]['name'] = $i;
            $res[$d]['no'] = $i;
          }
          $d++;
        }
    
        if ( $this->isNextPage )
        {
          $res = $this->make_before_next_link( $res, $act_page, $url, $param );
        }
        if ( $this->isFirstPage )
        {
          $res = $this->make_first_end_link( $res, $act_page, $url, $param );
        }
        return $res;
      }
      //// 带总页数
      function make_page_with_total( $total, $act_page, $url, $param )
      {
        $page_num = $this->Calculation_Page_Num( $total );
        $arr_min_max = $this->Calculation_Min_Max( $act_page );
        
        if (!eregi("([?|&]$param=)", $url)) {
          $url = strpos($url,"?")===false?$url."?":$url."&";
          $url = $url."$param=0";
        }
    
        if ( $act_page > $page_num )
        {
          $act_page = $page_num;
        }
        // 用正则把url改成正规的
        $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url );
    
        $res = array();
        $d = 0;
        for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ )
        {
          if ( $i == $act_page )
          {
            $res[$d]['url'] = '';
            $res[$d]['name'] = $i;
            $res[$d]['no'] = $i;
          }
          else
          {
            $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
            $res[$d]['name'] = $i;
            $res[$d]['no'] = $i;
          }
          $d++;
        }
    
        if ( $this->isNextPage )
        {
          $res = $this->make_before_next_link( $res, $act_page, $url, $param );
        }
        if ( $this->isFirstPage )
        {
          $res = $this->make_first_end_link( $res, $act_page, $url, $param );
        }
        
        $total_num= ceil($total/$this->length);
        $result['total']=$total_num;
        $result['DATA']=$res;
        return $result;
      }
      
      // 附加上一页和下一页
      function make_before_next_link( $arr, $act, $url, $param )
      {
        $tmp = array();
    
        $before = $act - 1;
        $next = $act + 1;
    
        if ( $before < 1 )
        {
          $before = 1;
          $tmp[0]['url'] = '';
          $tmp[0]['name'] = "上一页";
          $tmp[0]['no'] = $before;
        }
        else
        {
          $tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url );
          $tmp[0]['name'] = "上一页";
          $tmp[0]['no'] = $before;
        }
    
        $counts = sizeof( $arr );
        $tmp_count = sizeof( $tmp );
        for( $i = 0;$i < $counts;$i++ )
        {
          $tmp[$tmp_count]['url'] = $arr[$i]['url'];
          $tmp[$tmp_count]['name'] = $arr[$i]['name'];
          $tmp[$tmp_count]['no'] = $arr[$i]['no'];
          $tmp_count++;
        }
    
        if ( $next > $this->page_num )
        {
          $next = $this->page_num;
          $tmp[$tmp_count]['url'] = '';
          $tmp[$tmp_count]['name'] = "下一页";
          $tmp[$tmp_count]['no'] = $next;
        }
        else
        {
          $tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url );
          $tmp[$tmp_count]['name'] = "下一页";
          $tmp[$tmp_count]['no'] = $next;
        }
    
        return $tmp;
      }
      
      // 附加首页和尾页
      function make_first_end_link( $arr, $act, $url, $param )
      {
        $tmp = array();
    
        $before = 1;
        $next = $this->page_num;
    
        if ( $act == 1 )
        {
          $before = 1;
          $tmp[0]['url'] = '';
          $tmp[0]['name'] = "首页";
          $tmp[0]['no'] = $before;
        }
        else
        {
          $tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url );
          $tmp[0]['name'] = "首页";
          $tmp[0]['no'] = $before;
        }
    
        $counts = sizeof( $arr );
        $tmp_count = sizeof( $tmp );
        for( $i = 0;$i < $counts;$i++ )
        {
          $tmp[$tmp_count]['url'] = $arr[$i]['url'];
          $tmp[$tmp_count]['name'] = $arr[$i]['name'];
          $tmp[$tmp_count]['no'] = $arr[$i]['no'];
          $tmp_count++;
        }
    
        if ( $act == $this->page_num )
        {
          $tmp[$tmp_count]['url'] = '';
          $tmp[$tmp_count]['name'] = "尾页";
          $tmp[$tmp_count]['no'] = $next;
        }
        else
        {
          $tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url );
          $tmp[$tmp_count]['name'] = "尾页";
          $tmp[$tmp_count]['no'] = $next;
        }
    
        return $tmp;
      }
    ?>

    相关文章

    PHP速学教程(入门到精通)
    PHP速学教程(入门到精通)

    PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载

    相关标签:

    php

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    相关专题

    更多
    Java 桌面应用开发(JavaFX 实战)
    Java 桌面应用开发(JavaFX 实战)

    本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

    37

    2026.01.14

    php与html混编教程大全
    php与html混编教程大全

    本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

    19

    2026.01.13

    PHP 高性能
    PHP 高性能

    本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

    37

    2026.01.13

    MySQL数据库报错常见问题及解决方法大全
    MySQL数据库报错常见问题及解决方法大全

    本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

    19

    2026.01.13

    PHP 文件上传
    PHP 文件上传

    本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

    16

    2026.01.13

    PHP缓存策略教程大全
    PHP缓存策略教程大全

    本专题整合了PHP缓存相关教程,阅读专题下面的文章了解更多详细内容。

    6

    2026.01.13

    jQuery 正则表达式相关教程
    jQuery 正则表达式相关教程

    本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

    3

    2026.01.13

    交互式图表和动态图表教程汇总
    交互式图表和动态图表教程汇总

    本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

    45

    2026.01.13

    nginx配置文件详细教程
    nginx配置文件详细教程

    本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

    9

    2026.01.13

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    PHP课程
    PHP课程

    共137课时 | 8.6万人学习

    JavaScript ES5基础线上课程教学
    JavaScript ES5基础线上课程教学

    共6课时 | 7万人学习

    PHP新手语法线上课程教学
    PHP新手语法线上课程教学

    共13课时 | 0.9万人学习

    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号