Table of Contents
您可能感兴趣的文章
Home 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;
	}
}
Copy after login

类使用示例:

###处理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>
Copy after login

您可能感兴趣的文章

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



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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

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

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

How to implement data paging and display optimization in Vue projects How to implement data paging and display optimization in Vue projects Oct 15, 2023 am 09:27 AM

Implementing data paging and display optimization in Vue projects. In Vue projects, when a page needs to display a large amount of data, data paging and display optimization usually need to be performed to improve user experience. This article will introduce how to use Vue to implement data paging and display optimization. , and provide specific code examples. 1. Data paging Data paging refers to dividing a large amount of data into multiple pages according to certain rules and displaying them on the page. You can use the following steps to implement data paging in a Vue project: Define the data source. First, define a

Detailed explanation of JavaScript function return values ​​and return statements Detailed explanation of JavaScript function return values ​​and return statements Aug 04, 2022 am 09:46 AM

JavaScript functions provide two interfaces to interact with the outside world. The parameters serve as the entrance to receive external information; the return value serves as the outlet to feed back the operation results to the outside world. The following article will take you to understand the JavaScript function return value and briefly analyze the usage of the return statement. I hope it will be helpful to you!

See all articles