首页 php教程 php手册 仿Aspnetpager的一个PHP分页类 附源码下载

仿Aspnetpager的一个PHP分页类 附源码下载

Jun 06, 2016 pm 07:45 PM
php 分页

最近正在学习php,想拿一些demo来练习一下,想起前段时间自己用.net写的仿Aspnetpager控件的分页类,就琢磨着用PHP来实现仿aspnetpager的分页功能,随便复习下。 基本逻辑思路和.net的一样,就是将通过实体类来进行配置换成了通过数组进行配置,逻辑比较简单

最近正在学习php,想拿一些demo来练习一下,想起前段时间自己用.net写的仿Aspnetpager控件的分页类,就琢磨着用PHP来实现仿aspnetpager的分页功能,随便复习下。

基本逻辑思路和.net的一样,就是将通过实体类来进行配置换成了通过数组进行配置,逻辑比较简单,根据条件判断拼接分页html。

有以下几个简单的功能:

1:支持相关按钮的显示与否配置

3:支持url重写过的页面(需自己在配置数组中添加重写规则)

简单吧,还是直接上代码:

核心代码:pager.class.php

仿Aspnetpager的一个PHP分页类 附源码下载仿Aspnetpager的一个PHP分页类 附源码下载View Code

<span>php
    </span><span>class</span><span> pager{
        </span><span>//</span><span>分页的参数配置</span>
        <span>private</span> <span>$config</span>=<span>array</span><span>(
            </span><span>//</span><span>首页按钮的文本文字</span>
            "first_btn_text"=>"首页",
             <span>//</span><span>//</span><span>//</span><span>最后一页的文本文字,</span>
            "last_btn_text"=>"末页",
            <span>//</span><span>总记录数 *必需</span>
            "record_count"=>0,
            <span>//</span><span>每页分页尺寸</span>
            "pager_size"=>10,
            <span>//</span><span>当前页码  *必需</span>
            "pager_index"=>1,
            <span>//</span><span>每页显示的最大数量按钮</span>
            "max_show_page_size"=>10,
            <span>//</span><span>页码在浏览器中传值的名称  默认为page</span>
            "querystring_name"=>"page",
            <span>//</span><span>URL是否重写 默认为flase</span>
            "enable_urlrewriting"=><span>false</span>,
            <span>//</span><span>url重写规则 例如page/{page}  其中{page}就是代表页数</span>
            "urlrewrite_pattern"=>"",
            <span>//</span><span>分页容器的css名称</span>
            "classname"=>"paginator",
            <span>//</span><span>当前页按钮的class名称</span>
            "current_btn_class"=>"cpb",
            <span>//</span>
            "span_text_class"=>"stc",
            <span>/*</span><span>跳转的详细文本
             *totle代表总页数,
             *size代表每页数目
             * goto代表要跳转的输入框
             * record代表总记录数
             * index代表当前的页码
            </span><span>*/</span>
            "jump_info_text"=>"共{totle}页,每页{size}条记录,跳转到{goto}页",
            <span>//</span><span>跳转按钮的文本</span>
            "jump_btn_text"=>"确定",
            <span>//</span><span>是否显示跳转</span>
            "show_jump"=><span>false</span>,
            <span>//</span>
            "show_front_btn"=><span>true</span>,
            <span>//</span>
            "show_last_btn"=><span>true</span><span>
        );

        </span><span>/*</span><span>
         * 类的构造函数
         * $config:该分页类的配置
         </span><span>*/</span>
        <span>public</span> <span>function</span> __construct(<span>$config</span><span>)
        {
            </span><span>$this</span>->init_config(<span>$config</span><span>);
        }

        </span><span>function</span><span> __destruct()
        {
            </span><span>unset</span>(<span>$this</span>-><span>config);
        }

        </span><span>/*</span><span>
         * 构造分页主函数
         </span><span>*/</span>
        <span>public</span> <span>function</span><span> builder_pager()
        {
            </span><span>//</span><span>分页的字符串</span>
            <span>$pager_arr</span>=<span>array</span><span>();
            </span><span>//</span><span>每页的尺寸</span>
            <span>$pager_size</span>=<span>$this</span>->config["pager_size"<span>];
            </span><span>//</span><span>得到一共的分页数目</span>
            <span>$pager_num</span>=<span>$this</span>->config["record_count"]%<span>$pager_size</span>==0?<span>$this</span>->config["record_count"]/<span>$pager_size</span>:<span>floor</span>(<span>$this</span>->config["record_count"]/<span>$pager_size</span>)+1<span>;
            </span><span>//</span><span>当前的页码序号 如果是0,则置为1</span>
            <span>$pager_index</span>=<span>round</span>(<span>$this</span>->config["pager_index"])==0?1:<span>round</span>(<span>$this</span>->config["pager_index"<span>]);
            </span><span>//</span><span>如果当前的页码大于等于最后一页,则当前的页码置为最后一页</span>
            <span>$pager_index</span>=<span>$pager_index</span>>=<span>$pager_num</span>?<span>$pager_num</span>:<span>$pager_index</span><span>;
            </span><span>//</span>
            <span>$pager_next</span>=<span>$pager_index</span>>=<span>$pager_num</span>?<span>$pager_num</span>:(<span>$pager_index</span>+1<span>);
            </span><span>//</span><span>获取需要跳转 的url</span>
            <span>$url</span>=<span>$this</span>-><span>get_url();
            </span><span>//</span><span>添加开头的div</span>
            <span>$classname</span>=<span>$this</span>->config["classname"<span>];
            </span><span>$pager_arr</span>[]="<div class='\"<span'>$classname\">\n"<span>;
            </span><span>//</span><span>添加前面两个按钮的html</span>
            <span>if</span>(<span>$this</span>->config["show_front_btn"<span>])
            {
                </span><span>//</span><span>如果当前的页码为1 则front这两个按钮则会被禁用</span>
                <span>$attr</span>=<span>$pager_index</span>==1?"disabled=disabled":""<span>;
                </span><span>$pager_arr</span>[]=<span>$this</span>->get_a_html(self::format_url(<span>$url</span>,1),<span>$this</span>->config["first_btn_text"],<span>$attr</span><span>);
                </span><span>$pager_arr</span>[]=<span>$this</span>->get_a_html(self::format_url(<span>$url</span>,<span>$pager_index</span>-1),<span>$this</span>->config["pre_btn_text"],<span>$attr</span><span>);
            }

            </span><span>//</span><span>当前显示页码的起始 1~10 1  11~20 11</span>
            <span>$current_pager_start</span>=<span>$pager_index</span>%<span>$pager_size</span>==0?(<span>$pager_index</span>/<span>$pager_size</span>-1)*<span>$pager_size</span>+1:<span>floor</span>(<span>$pager_index</span>/<span>$pager_size</span>)*<span>$pager_size</span>+1<span>;
            </span><span>//</span><span>当前显示页码的结束</span>
            <span>$current_pager_end</span>=(<span>$current_pager_start</span>+<span>$pager_size</span>-1)>=<span>$pager_num</span>?<span>$pager_num</span>:(<span>$current_pager_start</span>+<span>$pager_size</span>-1<span>);

            </span><span>//</span><span>添加跳转到上一层的html</span>
            <span>if</span>(<span>$pager_index</span>><span>$pager_size</span><span>)
            {
                </span><span>$pager_arr</span>[]=<span>$this</span>->get_a_html(self::format_url(<span>$url</span>,<span>$current_pager_start</span>-1),"..."<span>);
            }

            </span><span>//</span><span>主体页码部分</span>
            <span>for</span>(<span>$i</span>=<span>$current_pager_start</span>;<span>$i</span>$current_pager_end;<span>$i</span>++<span>)
            {
                </span><span>if</span>(<span>$i</span>!=<span>$pager_index</span><span>)
                {
                    </span><span>$pager_arr</span>[]=<span>$this</span>->get_a_html(self::format_url(<span>$url</span>,<span>$i</span>),<span>$i</span><span>);

                }</span><span>else</span><span>{
                    </span><span>//</span><span>如果这个是当前页</span>
                    <span>$pager_arr</span>[]=<span>$this</span>->get_span_html(<span>$i</span>,<span>$this</span>->config["current_btn_class"<span>]);
                }
            }

            </span><span>//</span><span>添加下一层的html</span>
            <span>if</span>(<span>$pager_index</span>$pager_num-<span>$pager_num</span>%<span>$pager_size</span><span>))
            {
                </span><span>$pager_arr</span>[]=<span>$this</span>->get_a_html(self::format_url(<span>$url</span>,<span>$current_pager_end</span>+1),"..."<span>);
            }

            </span><span>//</span><span>添加后面两个按钮的html</span>
            <span>if</span>(<span>$this</span>->config["show_last_btn"<span>])
            {
                </span><span>//</span><span>如果当前的页码为最后一页 则last这两个按钮则会被禁用</span>
                <span>$attr</span>=<span>$pager_index</span>>=<span>$pager_num</span>?"disabled=disabled":""<span>;
                </span><span>$pager_arr</span>[]=<span>$this</span>->get_a_html(self::format_url(<span>$url</span>,<span>$pager_next</span>),<span>$this</span>->config["next_btn_text"],<span>$attr</span><span>);
                </span><span>$pager_arr</span>[]=<span>$this</span>->get_a_html(self::format_url(<span>$url</span>,<span>$pager_num</span>),<span>$this</span>->config["last_btn_text"],<span>$attr</span><span>);
            }

            </span><span>//</span><span>添加跳转的html</span>
            <span>if</span>(<span>$this</span>->config["show_jump"<span>])
            {
                </span><span>$patterns</span>=<span>array</span>("/\{totle\}/","/\{size\}/","/\{goto\}/","/\{record\}/","/\{index\}/",<span>);
                </span><span>$replacements</span>=<span>array</span><span>(
                    </span><span>$pager_num</span>,
                    <span>$pager_size</span>,
                    "<input type='\"input\"' id='\"jumpNum\"' style='\"width:20px;\"' name='\"jump\"' value='\"".<span'>$pager_next."\" />\n",
                    <span>$this</span>->config["record_count"],
                    <span>$this</span>->config["pager_index"<span>]
                );
                </span><span>//</span>
                <span>$pager_arr</span>[]=<span>preg_replace</span>(<span>$patterns</span>,<span>$replacements</span>,<span>$this</span>->config["jump_info_text"<span>]);
                </span><span>$btn_text</span>=<span>$this</span>->config['jump_btn_text'<span>];
                </span><span>$pager_arr</span>[]="<a href="%5C%22javascript:void(0);%5C%22" style='\"float:none;\"' onclick='\"javascript:jump();\"'>".<span>$this</span>->config['jump_btn_text']."</a>\n"<span>;
                </span><span>$pager_arr</span>[]=<span>$this</span>->get_jumpscript(<span>$url</span><span>);
            }

            </span><span>$pager_arr</span>[]="</div>"<span>;

            </span><span>$this</span>->config["pager_index"]=<span>$pager_index</span><span>;
            </span><span>return</span> <span>implode</span>(<span>$pager_arr</span><span>);
        }

        </span><span>/*</span><span>
         *获取需要处理的url,支持重写配置,各种参数的url
         </span><span>*/</span>
        <span>private</span> <span>function</span><span> get_url()
        {
            </span><span>//</span><span>如果设置了允许url重写</span>
            <span>if</span>(<span>$this</span>->config["enable_urlrewriting"<span>])
            {
                </span><span>//</span><span>得到调用文件所在的url</span>
                <span>$file_path</span>="http://".<span>$_SERVER</span>["HTTP_HOST"].<span>$_SERVER</span>["PHP_SELF"<span>];
                </span><span>//</span><span>得到调用url所在的网络目录</span>
                <span>$file_path</span>=<span>substr</span>(<span>$file_path</span>,0,<span>strripos</span>(<span>$file_path</span>,"/"))."/"<span>;
                </span><span>//</span><span>直接将目录附加重写规则  形成新的url</span>

                <span>$url</span>=<span>$file_path</span>.<span>$this</span>->config["urlrewrite_pattern"<span>];
            }</span><span>else</span><span>{
                </span><span>//</span><span>得到当前调用页面的绝对url</span>
                <span>$url</span>="http://".<span>$_SERVER</span>["HTTP_HOST"].<span>$_SERVER</span>["REQUEST_URI"<span>];
                </span><span>//</span><span>分页参数在浏览器中传递的名称</span>
                <span>$querystring_name</span>=<span>$this</span>->config['querystring_name'<span>];
                </span><span>//</span><span>如果该url中包含php?的字符串  则需要将分页参数替换</span>
                <span>if</span>(<span>strpos</span>(<span>$url</span>,"php?"<span>))
                {
                    </span><span>//</span><span>如果存在page=xxx的字样</span>
                    <span>$pattern</span>="/<span>$querystring_name</span>=[0-9]*/"<span>;
                    </span><span>if</span>(<span>preg_match</span>(<span>$pattern</span>,<span>$url</span><span>))
                    {
                        </span><span>//</span><span>将含page=***的字样中的数字替换成{0}</span>
                        <span>$url</span>=<span>preg_replace</span>(<span>$pattern</span>,"<span>$querystring_name</span>={page}",<span>$url</span><span>);
                    }</span><span>else</span><span>{
                        </span><span>$url</span>.="&<span>$querystring_name</span>={page}"<span>;
                    }

                }</span><span>else</span><span>{
                    </span><span>//</span><span>直接附加参数形成分页的完整url</span>
                    <span>$url</span>.="?<span>$querystring_name</span>={page}"<span>;
                }
            }
            </span><span>return</span> <span>$url</span><span>;
        }

        </span><span>/*</span><span>*/</span>
        <span>private</span> <span>static</span> <span>function</span> get_a_html(<span>$url</span>,<span>$title</span>,<span>$attr</span>=""<span>)
        {
            </span><span>return</span> "<a href="<span>%24url</span>">$attr style=\"margin-right:5px;\"><span>$title</span></a>\n"<span>;
        }

        </span><span>/*</span><span>*/</span>
        <span>private</span> <span>static</span> <span>function</span> get_span_html(<span>$num</span>,<span>$classname</span><span>)
        {
            </span><span>return</span> "<span class='\""' .>$classname</span>. "\"><span>$num</span>\n"<span>;
        }

        </span><span>/*</span><span>
         * 格式化url
         * $url 原url
         * $page 页码
         </span><span>*/</span>
        <span>private</span> <span>static</span> <span>function</span> format_url(<span>$url</span>,<span>$page</span><span>)
        {
            </span><span>return</span> <span>preg_replace</span>("/\{page\}$/",<span>$page</span>,<span>$url</span><span>);
        }

        </span><span>/*</span><span>
         *初始化分页的配置文件
         *如果在参数中不含该键值,则默认使用申明的值
         </span><span>*/</span>
        <span>private</span> <span>function</span> init_config(<span>$config</span><span>)
        {
            </span><span>//</span><span>判断该值是否存在、是否是数组、是否含有记录</span>
            <span>if</span>(<span>isset</span>(<span>$config</span>)&&<span>is_array</span>(<span>$config</span>)&&<span>count</span>(<span>$config</span>)>0<span>){
                </span><span>foreach</span>(<span>$config</span> <span>as</span> <span>$key</span>=><span>$val</span><span>)
                {
                    </span><span>$this</span>->config[<span>$key</span>]=<span>$val</span><span>;
                }
            }
        }

        </span><span>/*</span><span>
         * 构造跳转功能脚本的方法
         *$url:需要跳转的额那个url
         </span><span>*/</span>
        <span>private</span> <span>function</span> get_jumpscript(<span>$url</span><span>)
        {
            </span><span>$scriptstr</span> = "<script type='\"text/javascript\"'>\n".
                            "function jump(){\n".
                            "var jnum=document.getElementById(\"jumpNum\").value;\n".
                            "if(isNaN(jnum)){\n".
                            "alert(\"在跳转框中请输入数字!\");\n".
                            "}\n".
                            "else{\n".
                            "var re=/\{page\}/\n".
                            "location.href='<span>$url'.replace(re,jnum);\n".
                            "}\n".
                            "}\n".
                            "</script>\n"<span>;
            </span><span>return</span> <span>$scriptstr</span><span>;
        }

        </span><span>/*</span><span>
         * php中构造类似.net中format方法的函数
         * 用法:format("hello,{0},{1},{2}", 'x0','x1','x2')
         </span><span>*/</span>
        <span>private</span> <span>function</span><span> format() {

            </span><span>$args</span> = <span>func_get_args</span><span>();

            </span><span>if</span> (<span>count</span>(<span>$args</span>) == 0) { <span>return</span><span>;}

            </span><span>if</span> (<span>count</span>(<span>$args</span>) == 1) { <span>return</span> <span>$args</span>[0<span>]; }

            </span><span>$str</span> = <span>array_shift</span>(<span>$args</span><span>);

            </span><span>$str</span> = <span>preg_replace_callback</span>('/\\{(0|[1-9]\\d*)\\}/', <span>create_function</span>('$match', '$args = '.<span>var_export</span>(<span>$args</span>, <span>true</span>).'; return isset($args[$match[1]]) ? $args[$match[1]] : $match[0];'), <span>$str</span><span>);

            </span><span>return</span> <span>$str</span><span>;

        }

    }
</span>?>
登录后复制

直接用数组参数的方式调用

<span>php
            </span><span>$config1</span>=<span>array</span><span>(
                </span>"record_count"=>703,
                "pager_size"=>10,
                "show_jump"=><span>true</span>,
                "pager_index"=><span>$_GET</span>["page"<span>]
            );
            </span><span>$pager_simple</span>=<span>new</span> pager(<span>$config1</span><span>);
            </span><span>echo</span> <span>$pager_simple</span>-><span>builder_pager();
</span>?>
登录后复制

最后来看下demo的图片:

仿Aspnetpager的一个PHP分页类 附源码下载

由于小弟最近刚刚学习php,代码中出现的不规范,低效率,冗余或者设计问题还请大家多多指教。

demo源码下载

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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

在本章中,我们将了解CakePHP中的环境变量、常规配置、数据库配置和电子邮件配置。

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

See all articles