最近正在学习php,想拿一些demo来练习一下,想起前段时间自己用.net写的仿Aspnetpager控件的分页类,就琢磨着用PHP来实现仿aspnetpager的分页功能,随便复习下。 基本逻辑思路和.net的一样,就是将通过实体类来进行配置换成了通过数组进行配置,逻辑比较简单
最近正在学习php,想拿一些demo来练习一下,想起前段时间自己用.net写的仿Aspnetpager控件的分页类,就琢磨着用PHP来实现仿aspnetpager的分页功能,随便复习下。
基本逻辑思路和.net的一样,就是将通过实体类来进行配置换成了通过数组进行配置,逻辑比较简单,根据条件判断拼接分页html。
有以下几个简单的功能:
1:支持相关按钮的显示与否配置
3:支持url重写过的页面(需自己在配置数组中添加重写规则)
简单吧,还是直接上代码:
核心代码:pager.class.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的图片:
由于小弟最近刚刚学习php,代码中出现的不规范,低效率,冗余或者设计问题还请大家多多指教。
demo源码下载

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
