search
Homephp教程PHP源码一个简单的PHP分页函数

[PHP]代码   

<?php
/*
 * Created on 2011-07-28
 * Author : LKK , http://www.php.cn/
 * 使用方法:
	 require_once(&#39;mypage.php&#39;);
	 $result=mysql_query("select * from mytable", $myconn);
	 $total=mysql_num_rows($result);	//取得信息总数
	 pagepide($total,10);		//调用分页函数
	 
	 //数据库操作
	 $result=mysql_query("select * from mytable limit $sqlfirst,$shownu", $myconn);
	 while($row=mysql_fetch_array($result)){
	 	...您的操作
	 }
 	 echo $pagecon;	//输出分页导航内容
 */

if(!function_exists("pagepide")){
#$total		信息总数
#$shownu	显示数量,默认20
#$url		本页链接
function pagepide($total,$shownu=20,$url=&#39;&#39;){

	#$page  	当前页码
	#$sqlfirst 	mysql数据库起始项
	#$pagecon	分页导航内容
	global $page,$sqlfirst,$pagecon,$_SERVER;
	$GLOBALS["shownu"]=$shownu;
	
	if(isset($_GET[&#39;page&#39;])){
		$page=$_GET[&#39;page&#39;];
	}else $page=1;
	
	#如果$url使用默认,即空值,则赋值为本页URL
	if(!$url){ $url=$_SERVER["REQUEST_URI"];}
	
	#URL分析
	$parse_url=parse_url($url);
	@$url_query=$parse_url["query"];	//取出在问号?之后内容
	if($url_query){
		$url_query=preg_replace("/(&?)(page=$page)/","",$url_query);
		$url = str_replace($parse_url["query"],$url_query,$url);
		if($url_query){
		  $url .= "&page";
		}else $url .= "page";
	}else $url .= "?page";
	
	#页码计算
	$lastpg=ceil($total/$shownu);	//最后页,总页数
	$page=min($lastpg,$page);
	$prepg=$page-1; 				//上一页
	$nextpg=($page==$lastpg ? 0 : $page+1); //下一页
	$sqlfirst=($page-1)*$shownu;
	
	#开始分页导航内容
	$pagecon = "显示第 ".($total?($sqlfirst+1):0)."-".min($sqlfirst+$shownu,$total)." 条记录,共 <B>$total</B> 条记录";
	if($lastpg<=1) return false;	//如果只有一页则跳出
	
	if($page!=1) $pagecon .=" <a href=&#39;$url=1&#39;>首页</a> "; else $pagecon .=" 首页 ";
	if($prepg) $pagecon .=" <a href=&#39;$url=$prepg&#39;>前页</a> "; else $pagecon .=" 前页 ";
	if($nextpg) $pagecon .=" <a href=&#39;$url=$nextpg&#39;>后页</a> "; else $pagecon .=" 后页 ";
	if($page!=$lastpg) $pagecon.=" <a href=&#39;$url=$lastpg&#39;>尾页</a> "; else $pagecon .=" 尾页 ";
	
	#下拉跳转列表,循环列出所有页码
	$pagecon .=" 到第 <select name=&#39;topage&#39; size=&#39;1&#39; onchange=&#39;window.location=\"$url=\"+this.value&#39;>\n";
	for($i=1;$i<=$lastpg;$i++){
		if($i==$page) $pagecon .="<option value=&#39;$i&#39; selected>$i</option>\n";
		else $pagecon .="<option value=&#39;$i&#39;>$i</option>\n";
	}
	$pagecon .="</select> 页,共 $lastpg 页";

}
}else die(&#39;pagepide()同名函数已经存在!&#39;);
?>

                   

                   

Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use