Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求个具体的php+mysql分页-不要网上搜的

求个具体的php+mysql分页-不要网上搜的

Jun 23, 2016 pm 02:05 PM

一、从a页点击在b页进行分页显示
二、分页显示的代码
三、有 首页 上页 1 2 3 4 5 下页 末页的功能
四、翻页后能 正常显示内容就是点击上页下页能正常显示数据库的数据(a页传来的参数不丢失),


回复讨论(解决方案)

class page{	private $_page_num;//总页数	private $_page_size=10;//每页条数	private $_page_url;//url链接	private $_limit;//limit1	private $_page_total;//总分页数	private $_page;//分页所传分页值	private $_list_size=10;//这为第二种分页方法显示条数	private $_page_sort=3;//分页两边距离	public function __construct($_total,$_pagesize,$_list_size){		$this->_page_size=$_pagesize;		$this->_page_total=$_total;		$this->_list_size = $_list_size;		$this->_page_num = round($this->_page_total/$this->_page_size);		$this->_page=$this->getnum();		$this->_limit="limit".$this->_page*$this->_page_size.",".$this->_page_total=$_total;		$this->_page_url=$this->seturl();	}	/**	 * 获取当前页数	 * Enter description here ...	 */	private function getnum(){		if(isset($_GET['page'])){			if(is_numeric($_GET['page'])){				if($_GET['page']>0){					if(ceil($_GET['page'])>=1 && ceil($_GET['page'])<=$this->_page_num){						return ceil($_GET['page']);					}elseif(ceil($_GET['page'])>$this->_page_num){						return $this->_page_num;					}				}else{					return 1;				}			}else{				return 1;			}		}else{			return 1;		}	}	/**	 * 转换链接	 * Enter description here ...	 */	private function seturl(){		$_url=$_SERVER['REQUEST_URI'];//获取请求地址		$_par=parse_url($_url);//解析url		if(isset($_par['query'])){//判断是否存在aa=bb这种格式			$_url=parse_str($_par['query'],$_query);//重构url			unset($_query['page']);			$_url=$_par['path'].'?'.http_build_query($_query);		}else{			$_url=$_url.'?';		}		return $_url;	}		public function limit(){		return $this->_limit;	}		public function pagenum(){		return $this->_page_num;	}		/*	public function page(){		return $this->_page;	}	public function pageurl(){		return $this->_page_url;	}	*/		private function first(){		return "<a href=".$this->_page_url."&page=1>首页</a>";	}	private function end(){		return "<a href=".$this->_page_url."&page=".$this->_page_num.">尾页</a>";	}	private function prev(){		if($this->_page>1){			return "<a href=".$this->_page_url."&page=".($this->_page-1).">上一页</a>";		}else{			return '上一页';		}	}	public function next(){		if($this->_page<$this->_page_num){			return "<a href=".$this->_page_url."&page=".($this->_page+1).">下一页</a>";		}else{			return '下一页';		}	}	private function pagelist_1(){		$_page='';		for($i=1;$i<=$this->_page_num;$i++){			 $_page.="<a href=".$this->_page_url."&page=".$i.">".$i."</a>";		}		return $_page;	}	private function pagelist_2(){		$_page='';		$_page_start=floor($this->_page/$this->_list_size);		$usenum=$this->check(($_page_start*$this->_list_size+10),$this->_page_num);		for($i=$_page_start*$this->_list_size+1;$i<=$usenum;$i++){			$_page.="<a href=".$this->_page_url."&page=".$i.">".$i."</a>";			}		return $_page;	}	private function pagelist_3(){		$page='';		for($i=$this->_page-$this->_page_sort;$i<=$this->_page-1;$i++){			if($i<1)continue;			$page.="<a href=".$this->_page_url."&page=".$i.">".$i."</a>".' ';		}		$page.="<a href=".$this->_page_url."&page=".$this->_page.">".$this->_page."</a>".' ';		for($i=$this->_page+1;$i<=$this->_page+$this->_page_sort;$i++){			if($i>$this->_page_num)break;			$page.="<a href=".$this->_page_url."&page=".$i.">".$i."</a>".' ';		}		return $page;	}	/**	 * 没有修正的分页列表	 * Enter description here ...	 */	private function pagelist_4(){		$page='';		for($i=$this->_page-$this->_page_sort;$i<=$this->_page-1;$i++){			if($i<1)continue;			$page.="<a href=".$this->_page_url."&page=".$i.">".$i."</a>".' ';		}		$page.="<a href=".$this->_page_url."&page=".$this->_page.">".$this->_page."</a>".' ';		for($i=$this->_page+1;$i<=$this->_page+$this->_page_sort;$i++){			if($i>$this->_page_num)break;			$page.="<a href=".$this->_page_url."&page=".$i.">".$i."</a>".' ';		}		//+1和-1为了排除上一页显示的值,不然会重复		if($this->_page>($this->_page_sort+1) && $this->_page<($this->_page_num-$this->_page_sort)){			$page="<a href=".$this->_page_url."&page=".($this->_page-$this->_page_sort*2-1).">...</a>".$page."<a href=".$this->_page_url."&page=".($this->_page+$this->_page_sort*2+1).">...</a>";		}		return $page;	}	private function check($num1,$num2){		if($num1>$num2){			return $num2;		}else{			return $num1;		}	}	public function showpage($_type){		$_page='';		$_page.=$this->first();//首页		switch($_type){			case '1':				$_page.=$this->pagelist_1();//原始页面列表				break;			case '2':				$_page.=$this->pagelist_2();//原始页面列表				break;			case '3':				$_page.=$this->pagelist_3();//原始页面列表				break;			case '4':				$_page.=$this->pagelist_4();//原始页面列表				break;			default:				$_page.=$this->pagelist_3();//默认原始页面列表		}		$_page.=$this->end();//尾页		$_page.=$this->prev();//上一页		$_page.=$this->next();//下一页		return $_page;	}}//	$page = new page(220,10,10);//	echo $page->showpage('4');
Copy after login

这个好像可以用

不好意思,我是初学者,对类不太会用,现在是点击(如家电)后显示以下内容:
当前位置  |  首页  |   家电类  
内容1
内容2
内容3 
共 2页  第 1页  共 5 条记录 下一页 尾页
点下一页只显示:
当前位置  |  首页  |  类
共 8页  第 2页  共 30 条记录 首页上一页 下一页 尾页 

虽然不太懂,但我找到其他方法了

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles