Home Backend Development PHP Tutorial 适用好用的PHP分页类代码与使用

适用好用的PHP分页类代码与使用

Jun 13, 2016 pm 12:30 PM
gt page private return this

实用好用的PHP分页类代码与使用

类的代码如下:

<?php
	class Page {
??		private $total;??? 	//总记录
??		private $pagesize;??	//每页显示多少条
??		private $limit;???		//limit
??		private $page;???		//当前页码
??		private $pagenum;?		//总页码
??		private $url;????		//地址
??		private $bothnum;?		//两边保持数字分页的量
??
??	//构造方法初始化
??	public function __construct($_total, $_pagesize) {
???		$this->total = $_total ? $_total : 1;
???		$this->pagesize = $_pagesize;
??		$this->pagenum = ceil($this->total / $this->pagesize);
???		$this->page = $this->setPage();
???		$this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize";
???		$this->url = $this->setUrl();
???		$this->bothnum = 2;
??	}
??
??	//拦截器
??	private function __get($_key) {
???		return $this->$_key;
??	}
??
??	//获取当前页码
??	private function setPage() {
???		if (!empty($_GET['page'])) {
????			if ($_GET['page'] > 0) {
?????				if ($_GET['page'] > $this->pagenum) {
??????					return $this->pagenum;
?????				} else {
??????					return $_GET['page'];
?????				}
????			} else {
?????				return 1;
????			}
???		} else {
????			return 1;
???		}
??	}?
??
??	//获取地址
??	private function setUrl() {
???		$_url = $_SERVER["REQUEST_URI"];
???		$_par = parse_url($_url);
???		if (isset($_par['query'])) {
????			parse_str($_par['query'],$_query);
????			unset($_query['page']);
????			$_url = $_par['path'].'?'.http_build_query($_query);
???		}
???		return $_url;
??	}??	//数字目录
??	private function pageList() {
???		for ($i=$this->bothnum;$i>=1;$i--) {
????		$_page = $this->page-$i;
????		if ($_page < 1) continue;
????			$_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'">'.$_page.'</a> ';
???		}
???		$_pagelist .= ' <span class="me">'.$this->page.'</span> ';
???		for ($i=1;$i<=$this->bothnum;$i++) {
???	?		$_page = $this->page+$i;
????			if ($_page > $this->pagenum) break;
????			$_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'">'.$_page.'</a> ';
???		}
???		return $_pagelist;
??	}
??
??	//首页  http://ini.iteye.com/
??	private function first() {
???		if ($this->page > $this->bothnum+1) {
????			return ' <a href="'.$this->url.'">1</a> ...';
???		}
??	}
??
??	//上一页
??	private function prev() {
???		if ($this->page == 1) {
????			return '<span class="disabled">上一页</span>';
???		}
???		return ' <a href="'.$this->url.'&page='.($this->page-1).'">上一页</a> ';
??	}
??
??	//下一页
??	private function next() {
???		if ($this->page == $this->pagenum) {
????			return '<span class="disabled">下一页</span>';
???		}
???		return ' <a href="'.$this->url.'&page='.($this->page+1).'">下一页</a> ';
??	}
??
??	//尾页
??	private function last() {
???		if ($this->pagenum - $this->page > $this->bothnum) {
????			return ' ...<a href="'.$this->url.'&page='.$this->pagenum.'">'.$this->pagenum.'</a> ';
???		}
??	}
??
??	//分页信息
??	public function showpage() {
???		$_page .= $this->first();
???		$_page .= $this->pageList();
???		$_page .= $this->last();
???		$_page .= $this->prev();
???		$_page .= $this->next();
???		return $_page;
??	}
?}
?>
Copy after login

??使用说明:

<?php
	$_page = new Page($_total,$_pagesize); //其中 $_total 是数据集的总条数,$_pagesize 是每页显示的数量.
?>
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

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

What does private mean in java What does private mean in java Nov 24, 2022 pm 06:27 PM

In Java, private means "private" and is an access control modifier used to modify classes, properties and methods. Class members modified with private can only be accessed and modified by the methods of the class itself, and cannot be accessed and referenced by any other class (including subclasses of the class); therefore, the private modifier has the highest level of protection.

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

See all articles