目录
您可能感兴趣的文章
首页 php教程 php手册 PHP分页类分享

PHP分页类分享

Jun 21, 2016 am 08:46 AM
gt page return this

分享一个常用的php分页类。有三种表现形式,具体效果图如下:

(1)

(2)

(3)

该php分页类的具体代码以及使用方法如下:

/**
*PHP分页类
 *
*show(2) 1 ... 62 63 64 65 66 67 68 ... 150
*分页样式
*#page{font:12px/16px arial}
*#page span{float:left;margin:0px 3px;}
*#page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px
*7px;text-decoration:none;color:#666}
*#page a.now_page,#page a:hover{color:#fff;background:#05c}
 */
class Pager{
	public $first_row;//起始行数
	public $list_rows;//列表每页显示行数
	protected $total_pages;//总页数
	protected $total_rows;//总行数
	protected $now_page;//当前页数
	protected $method="defalut";//处理情况 Ajax分页 Html分页(静态化时) 普通get方式
	protected $parameter="";
	protected $page_name;//分页参数的名称
	protected $ajax_func_name;
	public $plus=3;//分页偏移量
	protected $url;
	/**
	*构造函数
	 *
	*@param unknown_type $data
	 */
	public function __construct($data=array()){
		$this->total_rows=$data["total_rows"];
		$this->parameter=!empty($data["parameter"])?$data["parameter"]:"";
		$this->list_rows=!empty($data["list_rows"])&&$data["list_rows"]<=100?$data["list_rows"]:15;
		$this->total_pages=ceil($this->total_rows / $this->list_rows);
		$this->page_name=!empty($data["page_name"])?$data["page_name"]:"page";
		$this->ajax_func_name=!empty($data["ajax_func_name"])?$data["ajax_func_name"]:"";
		$this->method=!empty($data["method"])?$data["method"]:"";
		/* 当前页面 */
		if(!empty($data["now_page"])){
			$this->now_page=intval($data["now_page"]);
		}else{
			$this->now_page=!empty($_GET[$this->page_name])?intval($_GET[$this->page_name]):1;
		}
		$this->now_page=$this->now_page<=0?1:$this->now_page;
		if(!empty($this->total_pages)&&$this->now_page>$this->total_pages){
			$this->now_page=$this->total_pages;
		}
		$this->first_row=$this->list_rows*($this->now_page-1);
	}
	/**
	*得到当前连接
	 *
	*@param
	*       	$page
	*@param
	*       	$text
	*@return string
	 */
	protected function _get_link($page,$text){
		switch ($this->method){
			case "ajax" :
				$parameter="";
				if($this->parameter){
					$parameter=",".$this->parameter;
				}
				return "<a  href="javascript:void(0)">".$text."</a>"."";
				break;
			case "html" :
				$url=str_replace("?",$page,$this->parameter);
				return "<a href="".$url."">".$text."</a>";
				break;

			default :
				return "<a href="".$this->_get_url($page)."">".$text."</a>";
				break;
		}
	}
	/**
	*设置当前页面链接
	 */
	protected function _set_url(){
		$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],"?")?"":"?").$this->parameter;
		$parse=parse_url($url);
		if(isset($parse["query"])){
			parse_str($parse["query"],$params);
			unset($params[$this->page_name]);
			$url=$parse["path"]."?".http_build_query($params);
		}
		if(!empty($params)){
			$url.="&";
		}
		$this->url=$url;
	}
	/**
	*得到$page的url
	 *
	*@param $page 页面
	*@return string
	 */
	protected function _get_url($page){
		if($this->url === NULL){
			$this->_set_url();
		}
		//	$lable=strpos("&",$this->url) === FALSE?"":"&";
		return $this->url.$this->page_name."=".$page;
	}
	/**
	*得到第一页
	 *
	*@return string
	 */
	public function first_page($name="第一页"){
		if($this->now_page>5){
			return $this->_get_link("1",$name);
		}
		return "";
	}
	/**
	*最后一页
	 *
	*@param
	*       	$name
	*@return string
	 */
	public function last_page($name="最后一页"){
		if($this->now_page<$this->total_pages-5){
			return $this->_get_link($this->total_pages,$name);
		}
		return "";
	}
	/**
	*上一页
	 *
	*@return string
	 */
	public function up_page($name="上一页"){
		if($this->now_page!=1){
			return $this->_get_link($this->now_page-1,$name);
		}
		return "";
	}
	/**
	*下一页
	 *
	*@return string
	 */
	public function down_page($name="下一页"){
		if($this->now_page<$this->total_pages){
			return $this->_get_link($this->now_page+1,$name);
		}
		return "";
	}
	/**
	*分页样式输出
	 *
	*@param
	*       	$param
	*@return string
	 */
	public function show($param=1){
		if($this->total_rows<1){
			return "";
		}
		$className="show_".$param;
		$classNames=get_class_methods($this);
		if(in_array($className,$classNames)){
			return $this->$className();
		}
		return "";
	}
	protected function show_2(){
		if($this->total_pages!=1){
			$return="";
			$return.=$this->up_page("<");
			for($i=1;$i<=$this->total_pages;$i++){
				if($i==$this->now_page){
					$return.="<a class="now_page">$i</a>";
				}else{
					if($this->now_page-$i>=4&&$i!=1){
						$return.="<span class="pageMore">...</span>";
						$i=$this->now_page-3;
					}else{
						if($i>=$this->now_page+5&&$i!=$this->total_pages){
							$return.="<span>...</span>";
							$i=$this->total_pages;
						}
						$return.=$this->_get_link($i,$i);
					}
				}
			}
			$return.=$this->down_page(">");
			return $return;
		}
	}
	protected function show_1(){
		$plus=$this->plus;
		if($plus+$this->now_page>$this->total_pages){
			$begin=$this->total_pages-$plus*2;
		}else{
			$begin=$this->now_page-$plus;
		}

		$begin=($begin>=1)?$begin:1;
		$return="";
		$return.=$this->first_page();
		$return.=$this->up_page();
		for($i=$begin;$i<=$begin+$plus*2;$i++){
			if($i>$this->total_pages){
				break;
			}
			if($i==$this->now_page){
				$return.="<a class="now_page">$i</a>";
			}else{
				$return.=$this->_get_link($i,$i);
			}
		}
		$return.=$this->down_page();
		$return.=$this->last_page();
		return $return;
	}
	protected function show_3(){
		$plus=$this->plus;
		if($plus+$this->now_page>$this->total_pages){
			$begin=$this->total_pages-$plus*2;
		}else{
			$begin=$this->now_page-$plus;
		}
		$begin=($begin>=1)?$begin:1;
		$return="总计 ".$this->total_rows." 个记录分为 ".$this->total_pages." 页,当前第 ".$this->now_page." 页 ";
		$return.=",每页 ";
		$return.="<input type="text" value="".$this->list_rows."" id="pageSize" size="3"> ";
		$return.=$this->first_page();
		$return.=$this->up_page();
		$return.=$this->down_page();
		$return.=$this->last_page();
		$return.="<select onchange="".$this->ajax_func_name."(this.value)" id="gotoPage">";
		for($i=$begin;$i<=$begin+10;$i++){
			if($i>$this->total_pages){
				break;
			}
			if($i==$this->now_page){
				$return.="<option selected="true" value="".$i."">".$i."</option>";
			}else{
				$return.="<option value="".$i."">".$i."</option>";
			}
		}
		$return.="</select>";
		return $return;
	}
}
登录后复制

类使用示例:

###处理html静态化页面分页的情况###
# method 处理环境 设置为 html
# parameter 为静态页面参数  www.Alixixi.com/20-0-0-0-40-?.html 注意问号
# ?问号的位置会自动替换为去向页码
# now_page 当前页面(静态页面获取不到当前页面所以只有你传入)
$params=array(
			"total_rows"=>100,#(必须)
			"method"    =>"html",#(必须)
			"parameter" =>"www.Alixixi.com/20-0-0-0-40-?.html", #(必须)
			"now_page"  =>$_GET["p"], #(必须)
			"list_rows" =>10,#(可选) 默认为15
);
$page=new Pager($params);
   echo $page->show(1);
   #<a href="www.Alixixi.com/20-0-0-0-40-2.html">2</a>

###处理ajax分页的情况###
# method 处理环境 设置为 ajax
# ajax_func_name ajax分页跳转页面的javascript方法
# parameter    ajax_func_name后面的附带参数 默认为空
# now_page 不到当前页面所以只有你传入
$params=array(
			"total_rows"=>100,
			"method"    =>"ajax",
			"ajax_func_name" =>"goToPage",
			"now_page"  =>1,
			#"parameter" =>""jiong","username"",
);
$page=new Pager($params);
echo $page->show(1);
#<a href="javascript:void(0)" >7</a>
#添加了parameter<a href="javascript:void(0)" >6</a>
登录后复制

您可能感兴趣的文章

  • 在php中分别使用curl的post提交数据的方法和get获取网页数据的方法总结
  • PHP分析文件头信息判断上传文件的类型
  • php判断变量类型常用方法
  • PHP 利用 Curl Functions 实现多线程抓取网页和下载文件
  • php控制请求页面浏览器缓存
  • PHP判断浏览器类型
  • PHP Curl批量多线程打开网址的类
  • php通过socket获取网页内容的简单示例



本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系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.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 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)

华为GT3 Pro和GT4的差异是什么? 华为GT3 Pro和GT4的差异是什么? Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

C语言return的用法详解 C语言return的用法详解 Oct 07, 2023 am 10:58 AM

C语言return的用法有:1、对于返回值类型为void的函数,可以使用return语句来提前结束函数的执行;2、对于返回值类型不为void的函数,return语句的作用是将函数的执行结果返回给调用者;3、提前结束函数的执行,在函数内部,我们可以使用return语句来提前结束函数的执行,即使函数并没有返回值。

修复:截图工具在 Windows 11 中不起作用 修复:截图工具在 Windows 11 中不起作用 Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

Java中return和finally语句的执行顺序是怎样的? Java中return和finally语句的执行顺序是怎样的? Apr 25, 2023 pm 07:55 PM

源码:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#输出上述代码的输出可以简单地得出结论:return在finally之前执行,我们来看下字节码层面上发生了什么事情。下面截取case1方法的部分字节码,并且对照源码,将每个指令的含义注释在

如何修复无法连接到iPhone上的App Store错误 如何修复无法连接到iPhone上的App Store错误 Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Vue项目中如何实现数据的分页和显示优化 Vue项目中如何实现数据的分页和显示优化 Oct 15, 2023 am 09:27 AM

Vue项目中实现数据的分页和显示优化在Vue项目中,当页面需要展示大量数据时,通常需要进行数据的分页和显示优化以提高用户体验,本文将介绍如何使用Vue实现数据的分页和显示优化,并提供具体的代码示例。一、数据分页数据分页是指将大量数据按照一定的规则分割成多页,并在页面上进行分页显示。Vue项目中可以使用如下步骤来实现数据分页:定义数据源首先,定义一个包含所有数

Vue3怎么使用setup语法糖拒绝写return Vue3怎么使用setup语法糖拒绝写return May 12, 2023 pm 06:34 PM

Vue3.2setup语法糖是在单文件组件(SFC)中使用组合式API的编译时语法糖解决Vue3.0中setup需要繁琐将声明的变量、函数以及import引入的内容通过return向外暴露,才能在使用的问题1.在使用中无需return声明的变量、函数以及import引入的内容,即可在使用语法糖//import引入的内容import{getToday}from'./utils'//变量constmsg='Hello!'//函数func

See all articles