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

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

Jun 06, 2016 pm 07:45 PM
php penomboran

最近正在学习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>?>
Salin selepas log masuk

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

<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>?>
Salin selepas log masuk

最后来看下demo的图片:

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

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

demo源码下载

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Artikel Panas

Repo: Cara menghidupkan semula rakan sepasukan
1 bulan yang lalu By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Kristal tenaga dijelaskan dan apa yang mereka lakukan (kristal kuning)
2 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Cara mendapatkan biji gergasi
1 bulan yang lalu By 尊渡假赌尊渡假赌尊渡假赌

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Konfigurasi Projek CakePHP Konfigurasi Projek CakePHP Sep 10, 2024 pm 05:25 PM

Dalam bab ini, kita akan memahami Pembolehubah Persekitaran, Konfigurasi Umum, Konfigurasi Pangkalan Data dan Konfigurasi E-mel dalam CakePHP.

Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 membawa beberapa ciri baharu, peningkatan keselamatan dan peningkatan prestasi dengan jumlah penamatan dan penyingkiran ciri yang sihat. Panduan ini menerangkan cara memasang PHP 8.4 atau naik taraf kepada PHP 8.4 pada Ubuntu, Debian, atau terbitan mereka

Tarikh dan Masa CakePHP Tarikh dan Masa CakePHP Sep 10, 2024 pm 05:27 PM

Untuk bekerja dengan tarikh dan masa dalam cakephp4, kami akan menggunakan kelas FrozenTime yang tersedia.

Muat naik Fail CakePHP Muat naik Fail CakePHP Sep 10, 2024 pm 05:27 PM

Untuk mengusahakan muat naik fail, kami akan menggunakan pembantu borang. Di sini, adalah contoh untuk muat naik fail.

Penghalaan CakePHP Penghalaan CakePHP Sep 10, 2024 pm 05:25 PM

Dalam bab ini, kita akan mempelajari topik berikut yang berkaitan dengan penghalaan ?

Bincangkan CakePHP Bincangkan CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP ialah rangka kerja sumber terbuka untuk PHP. Ia bertujuan untuk menjadikan pembangunan, penggunaan dan penyelenggaraan aplikasi lebih mudah. CakePHP adalah berdasarkan seni bina seperti MVC yang berkuasa dan mudah difahami. Model, Pandangan dan Pengawal gu

Pengesah Mencipta CakePHP Pengesah Mencipta CakePHP Sep 10, 2024 pm 05:26 PM

Pengesah boleh dibuat dengan menambah dua baris berikut dalam pengawal.

Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Dec 20, 2024 am 11:31 AM

Kod Visual Studio, juga dikenali sebagai Kod VS, ialah editor kod sumber percuma — atau persekitaran pembangunan bersepadu (IDE) — tersedia untuk semua sistem pengendalian utama. Dengan koleksi sambungan yang besar untuk banyak bahasa pengaturcaraan, Kod VS boleh menjadi c

See all articles