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

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

Jun 13, 2016 am 11:57 AM
.net php Same code Pagination and Basic Ideas of kind logic

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

有以下几个简单的功能:

1:支持相关按钮的显示与否配置
2:支持每页数目,文本名称,html标签类名称的自由配置
3:支持url重写过的页面(需自己在配置数组中添加重写规则)

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

核心代码:pager.class.php

复制代码 代码如下:


class pager{
//分页的参数配置
private $config=array(
//首页按钮的文本文字
"first_btn_text"=>"首页",
//上一页按钮的文本文字,
"pre_btn_text"=>"上一页",
//下一页的文本文字
"next_btn_text"=>"下一页",
//最后一页的文本文字,
"last_btn_text"=>"末页",
//总记录数 *必需
"record_count"=>0,
//每页分页尺寸
"pager_size"=>10,
//当前页码 *必需
"pager_index"=>1,
//每页显示的最大数量按钮
"max_show_page_size"=>10,
//页码在浏览器中传值的名称 默认为page
"querystring_name"=>"page",
//URL是否重写 默认为flase
"enable_urlrewriting"=>false,
//url重写规则 例如page/{page} 其中{page}就是代表页数
"urlrewrite_pattern"=>"",
//分页容器的css名称
"classname"=>"paginator",
//当前页按钮的class名称
"current_btn_class"=>"cpb",
//分页文字描述span标签的css
"span_text_class"=>"stc",
/*跳转的详细文本
*totle代表总页数,
*size代表每页数目
* goto代表要跳转的输入框
* record代表总记录数
* index代表当前的页码
*/
"jump_info_text"=>"共{totle}页,每页{size}条记录,跳转到{goto}页",
//跳转按钮的文本
"jump_btn_text"=>"确定",
//是否显示跳转
"show_jump"=>false,
//是否展示前面的按钮 首页&上一页
"show_front_btn"=>true,
//是否展示后面的按钮 下一页&末页
"show_last_btn"=>true
);
/*
* 类的构造函数
* $config:该分页类的配置
*/
public function __construct($config)
{
$this->init_config($config);
}
function __destruct()
{
unset($this->config);
}
/*
* 构造分页主函数
*/
public function builder_pager()
{
//分页的字符串
$pager_arr=array();
//每页的尺寸
$pager_size=$this->config["pager_size"];
//得到一共的分页数目
$pager_num=$this->config["record_count"]%$pager_size==0?$this->config["record_count"]/$pager_size:floor($this->config["record_count"]/$pager_size)+1;
//当前的页码序号 如果是0,则置为1
$pager_index=round($this->config["pager_index"])==0?1:round($this->config["pager_index"]);
//如果当前的页码大于等于最后一页,则当前的页码置为最后一页
$pager_index=$pager_index>=$pager_num?$pager_num:$pager_index;
//下一页的页码
$pager_next=$pager_index>=$pager_num?$pager_num:($pager_index+1);
//获取需要跳转 的url
$url=$this->get_url();
//添加开头的div
$classname=$this->config["classname"];
$pager_arr[]="

\n";
//添加前面两个按钮的html
if($this->config["show_front_btn"])
{
//如果当前的页码为1 则front这两个按钮则会被禁用
$attr=$pager_index==1?"disabled=disabled":"";
$pager_arr[]=$this->get_a_html(self::format_url($url,1),$this->config["first_btn_text"],$attr);
$pager_arr[]=$this->get_a_html(self::format_url($url,$pager_index-1),$this->config["pre_btn_text"],$attr);
}
//当前显示页码的起始 1~10 1 11~20 11
$current_pager_start=$pager_index%$pager_size==0?($pager_index/$pager_size-1)*$pager_size+1:floor($pager_index/$pager_size)*$pager_size+1;
//当前显示页码的结束
$current_pager_end=($current_pager_start+$pager_size-1)>=$pager_num?$pager_num:($current_pager_start+$pager_size-1);
//添加跳转到上一层的html
if($pager_index>$pager_size)
{
$pager_arr[]=$this->get_a_html(self::format_url($url,$current_pager_start-1),"...");
}
//主体页码部分
for($i=$current_pager_start;$i{
if($i!=$pager_index)
{
$pager_arr[]=$this->get_a_html(self::format_url($url,$i),$i);
}else{
//如果这个是当前页
$pager_arr[]=$this->get_span_html($i,$this->config["current_btn_class"]);
}
}
//添加下一层的html
if($pager_index{
$pager_arr[]=$this->get_a_html(self::format_url($url,$current_pager_end+1),"...");
}
//添加后面两个按钮的html
if($this->config["show_last_btn"])
{
//如果当前的页码为最后一页 则last这两个按钮则会被禁用
$attr=$pager_index>=$pager_num?"disabled=disabled":"";
$pager_arr[]=$this->get_a_html(self::format_url($url,$pager_next),$this->config["next_btn_text"],$attr);
$pager_arr[]=$this->get_a_html(self::format_url($url,$pager_num),$this->config["last_btn_text"],$attr);
}
//添加跳转的html
if($this->config["show_jump"])
{
$patterns=array("/\{totle\}/","/\{size\}/","/\{goto\}/","/\{record\}/","/\{index\}/",);
$replacements=array(
$pager_num,
$pager_size,
"\n",
$this->config["record_count"],
$this->config["pager_index"]
);
//替换特定的标签组成跳转
$pager_arr[]=preg_replace($patterns,$replacements,$this->config["jump_info_text"]);
$btn_text=$this->config['jump_btn_text'];
$pager_arr[]="".$this->config['jump_btn_text']."\n";
$pager_arr[]=$this->get_jumpscript($url);
}
$pager_arr[]="
";
$this->config["pager_index"]=$pager_index;
return implode($pager_arr);
}
/*
*获取需要处理的url,支持重写配置,各种参数的url
*/
private function get_url()
{
//如果设置了允许url重写
if($this->config["enable_urlrewriting"])
{
//得到调用文件所在的url
$file_path="http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
//得到调用url所在的网络目录
$file_path=substr($file_path,0,strripos($file_path,"/"))."/";
//直接将目录附加重写规则 形成新的url
$url=$file_path.$this->config["urlrewrite_pattern"];
}else{
//得到当前调用页面的绝对url
$url="http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
//分页参数在浏览器中传递的名称
$querystring_name=$this->config['querystring_name'];
//如果该url中包含php?的字符串 则需要将分页参数替换
if(strpos($url,"php?"))
{
//如果存在page=xxx的字样
$pattern="/$querystring_name=[0-9]*/";
if(preg_match($pattern,$url))
{
//将含page=***的字样中的数字替换成{0}
$url=preg_replace($pattern,"$querystring_name={page}",$url);
}else{
$url.="&$querystring_name={page}";
}
}else{
//直接附加参数形成分页的完整url
$url.="?$querystring_name={page}";
}
}
return $url;
}
/*
* 得到a标签的html
*$url:a标签所要导向的html
*$title:a标签的标题
**$attr:a标签上的附加属性 可以不写
*/
private static function get_a_html($url,$title,$attr="")
{
return "$title\n";
}
/*
* 获得span标签的html
* $num:span中的文本,即页序号
* $classname:span标签的class名称
*/
private static function get_span_html($num,$classname)
{
return "$num\n";
}
/*
* 格式化url
* $url 原url
* $page 页码
*/
private static function format_url($url,$page)
{
return preg_replace("/\{page\}$/",$page,$url);
}
/*
*初始化分页的配置文件
*如果在参数中不含该键值,则默认使用申明的值
*/
private function init_config($config)
{
//判断该值是否存在、是否是数组、是否含有记录
if(isset($config)&&is_array($config)&&count($config)>0){
foreach($config as $key=>$val)
{
$this->config[$key]=$val;
}
}
}
/*
* 构造跳转功能脚本的方法
*$url:需要跳转的额那个url
*/
private function get_jumpscript($url)
{
$scriptstr = "\n";
return $scriptstr;
}
/*
* php中构造类似.net中format方法的函数
* 用法:format("hello,{0},{1},{2}", 'x0','x1','x2')
*/
private function format() {
$args = func_get_args();
if (count($args) == 0) { return;}
if (count($args) == 1) { return $args[0]; }
$str = array_shift($args);
$str = preg_replace_callback('/\\{(0|[1-9]\\d*)\\}/', create_function('$match', '$args = '.var_export($args, true).'; return isset($args[$match[1]]) ? $args[$match[1]] : $match[0];'), $str);
return $str;
}
}
?>

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

复制代码 代码如下:


$config1=array(
"record_count"=>703,
"pager_size"=>10,
"show_jump"=>true,
"pager_index"=>$_GET["page"]
);
$pager_simple=new pager($config1);
echo $pager_simple->builder_pager();
?>


最后来看下demo的图片:

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

demo源码下载
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

See all articles