首頁 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.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前 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)

適用於 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

如何設定 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 擁有大量針對多種程式語言的擴展,可以輕鬆編寫

我後悔之前不知道的 7 個 PHP 函數 我後悔之前不知道的 7 個 PHP 函數 Nov 13, 2024 am 09:42 AM

如果您是經驗豐富的PHP 開發人員,您可能會感覺您已經在那裡並且已經完成了。操作

您如何在PHP中解析和處理HTML/XML? 您如何在PHP中解析和處理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

php程序在字符串中計數元音 php程序在字符串中計數元音 Feb 07, 2025 pm 12:12 PM

字符串是由字符組成的序列,包括字母、數字和符號。本教程將學習如何使用不同的方法在PHP中計算給定字符串中元音的數量。英語中的元音是a、e、i、o、u,它們可以是大寫或小寫。 什麼是元音? 元音是代表特定語音的字母字符。英語中共有五個元音,包括大寫和小寫: a, e, i, o, u 示例 1 輸入:字符串 = "Tutorialspoint" 輸出:6 解釋 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。總共有 6 個元

解釋PHP中的晚期靜態綁定(靜態::)。 解釋PHP中的晚期靜態綁定(靜態::)。 Apr 03, 2025 am 12:04 AM

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

什麼是PHP魔術方法(__ -construct,__destruct,__call,__get,__ set等)並提供用例? 什麼是PHP魔術方法(__ -construct,__destruct,__call,__get,__ set等)並提供用例? Apr 03, 2025 am 12:03 AM

PHP的魔法方法有哪些? PHP的魔法方法包括:1.\_\_construct,用於初始化對象;2.\_\_destruct,用於清理資源;3.\_\_call,處理不存在的方法調用;4.\_\_get,實現動態屬性訪問;5.\_\_set,實現動態屬性設置。這些方法在特定情況下自動調用,提升代碼的靈活性和效率。

See all articles