php通用分页种

Jun 13, 2016 am 11:03 AM
function gt page param this

php通用分页类

<?phpinterface ILink{	public function parse($page,$param);}?>
Copy after login

?

<?phprequire 'ILink.php';class LinkAdapter implements ILink{	/**	 * @param unknown_type $page	 * @param unknown_type $param	 */	public function parse($page, $param) 	{				$temp="共{$page->getAllPage()}页,第{$page->getCurrentPage()}页 ";		$links=$this->getLinkString($param);		if($page->hasPrevious())$temp.="<a href="%24links=%22.(%24page->getCurrentPage()-1).%22">上一页</a> ";else{$temp.="上一页 ";}		for($i=$page->getCurrentPage();$igetAllPage()&&$igetPerRecords();$i++)		{			$temp.="<a href="%24links=%24i">{$i}</a> ";		}		if($page->hasNext())$temp.="<a href="%24links=%22.(%24page->getCurrentPage()+1).%22">下一页</a> ";else{$temp.="下一页 ";}		return $temp;	}	public function getLinkString($param)	{		$str="";		$attr=$_GET;		unset($attr[$param]);		if($attr)		{			foreach($attr as $key=>$val)			{				if($str=="")				{					$str.="?$key=$val";				}				else				{					$str.="&$key=$val";				}			}			$str.="&$param";		}		else		{			$str.="?$param";		}		return $str;	}}?>
Copy after login

??

<?phpclass Page {	private $allPage;#总页数	private $allRecords;#总记录数	private $perRecords;#单页记录数	private $currentPage=1;#当前页面	/**	 * @return the $allPage	 */	public function getAllPage()	{		return $this->allPage;	}	/**	 * @return the $allRecords	 */	public function getAllRecords() 	{		return $this->allRecords;	}	/**	 * @return the $perRecords	 */	public function getPerRecords() {		return $this->perRecords;	}	/**	 * @return the $currentPage	 */	public function getCurrentPage() 	{		return $this->currentPage;	}	/**	 * @param $allPage the $allPage to set	 */	public function setAllPage($allPage) 	{		$this->allPage = ($allPage%$this->perRecords == 0)?($allPage/$this->perRecords):($allPage/$this->perRecords+1);		$this->allPage=intval($this->allPage);	}	/**	 * @param $allRecords the $allRecords to set	 */	public function setAllRecords($allRecords) 	{		$this->allRecords = $allRecords;	}	/**	 * @param $perRecords the $perRecords to set	 */	public function setPerRecords($perRecords) {		$this->perRecords = $perRecords;	}	/**	 * @param $currentPage the $currentPage to set	 */	public function setCurrentPage($currentPage) 	{		if ($currentPage currentPage = 1;		else if ($currentPage > $this->allPage)			$this->currentPage =$this->allPage;		else			$this->currentPage=$currentPage;	}	public function hasNext() 	{		return $this->currentPageallPage;	}	public function hasPrevious() 	{		return $this->currentPage>1;	}	public function getEndIndex() 	{		return ((($this->currentPage-1)*$this->perRecords)+$this->perRecords)>$this->allRecords?((($this->currentPage-1)*$this->perRecords)+$this->perRecords)-$this->allRecords:$this->perRecords;	}	public function getStartIndex() 	{		return ($this->currentPage-1)*$this->perRecords;	}}?>
Copy after login

?

<?phprequire 'Page.php';require'LinkAdapter.php';class Pager {	private $list=array();	private $page;#分页对象	private $param;#页面请求参数	public function __construct($list)	{		$this->list=$list;		$this->page=new Page();	}	/**	 * 	 * @param unknown_type $rows 显示的数据量	 * @param unknown_type $current 当前页	 */	public function init($rows=5,$current)	{		$this->page->setAllRecords(count($this->list));		$this->page->setPerRecords($rows);		$this->page->setAllPage(count($this->list));		$this->page->setCurrentPage($current);				$this->list=array_slice($this->list,$this->page->getStartIndex(),$this->page->getEndIndex());	}	/**	 * 获取分页变量	 */	public function getVar()	{		return $this->list;	}	/**	 * @return the $param	 */	public function getParam() 	{		return $this->param;	}	/**	 * @param $param the $param to set	 */	public function setParam($param) {		$this->param = $param;	}	/**	 * 加载插件信息,获取生成的链接,装饰器模式	 * @param unknown_type $link	 */	public function getLink($link=null)	{		if(!empty($link)||!(($link instanceof ILink)))$link=new LinkAdapter();		return $link->parse($this->page,$this->param);	}}?>
Copy after login

?

<?php include'lib/Pager.php';	$target=array();	for($i=0;$i<=100;$i++){$target[]=$i;}	$page=new Pager($target);	$page->setParam("page");	$page->init(30,$_REQUEST['page']);	$list=$page->getVar();	foreach($list as $val):		echo $val.'<br>';	endforeach;	echo $page->getLink();?>
Copy after login

?下载

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
4 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

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

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

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

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

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

See all articles