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)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

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.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles