当前位置: 首页 > 图文教程 > 网络编程 > PHP > 一个好用的分页函数

PHP
MYSQL版本大于4.1问题 - PHPchina
怎么让用户点击一个连接后,把一个图片另存了 - PHPchina
武汉10月15日Phper聚会召集!!! - PHPchina
php如果不等待exec执行的程序创建的子进程? - PHPchina
哪位知道DISCUZ处理防SQL注入的代码是哪部分 - PHPchina
求教!我实在不知道哪里问题,在线等ing - PHPchina
怎样结束用户某一进程 - PHPchina
比对用户名密码能不能这样写? - PHPchina
求助:如何在PHP+mysql中实现数据备份? - PHPchina
大家看看这个配置对吗 - PHPchina
如何禁止require当前文件 - PHPchina
无法将回调函数放在类中? - PHPchina
村里 PHP代码高亮是怎么实现的? - PHPchina
apache安装后.服务里没有apache2这个服务! - PHPchina
请教一个小问题 - PHPchina
config.php里面是不是应该把多数参数设置为常量而不是变量? - PHPchina
请教高手一个问题 - PHPchina
如何让百度收录我的网站 ?? - PHPchina
谁能给个注入的简单语句? - PHPchina
求PHP站内搜索思路 - PHPchina

PHP 中的 一个好用的分页函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 331 ::
收藏到网摘: n/a

复制代码 代码如下:

本人原创的代码,高手看来,也许流程笨拙点,但是很实用.看者要顶啊
/*---------------------------------------------------------------//
* 函数说明:分页函数 page($sql,$pagesize="30")
* $sql 查询语句(除limit外,可带排序或者条件限制)
* 如 select * from stu where time between "1" and "30";
* $pagesize 每页的显示条数
* ## 可输出数组$arr的值,说明如下:
* $arr["first"] 首页及地址
* $arr["page_pre"] 上一页及地址
* $arr["all"] 当第几页和总页数
* $arr["page_next"]下一页及地址
* $arr["last"] 末页及地址
* $arr["pagelist"] 页码列表及地址,显示当前页前后4页列表
* $arr["query"] 语句 $arr["query"] = mysql_query($sql)
* $arr["nums"] 记录总数
* 2006.09.06 by Kevin QQ:84529890
//----------------------------------------------------------------*/
function page($sql,$pagesize="30"){
global $arr,$PHP_SELF;
$query = mysql_query($sql);
$num = mysql_num_rows($query);
$pagecount = ceil($num/$pagesize);
$page = $_GET["page"];
if(!$page) $page=1;
if($page>$pagecount) $page = $pagecount;
$offset = ($page-1)*$pagesize;
$sql.=" limit $offset , $pagesize";
$arr["query"] = mysql_query($sql);
if($page>1){
$page_pre = $page-1;
$page_url = $PHP_SELF . "?page=".$page_pre;
$arr["page_pre"] = "<a href=\"".$page_url."\">上一页|</a>\n";
}
if($page<$pagecount){
$page_next = $page+1;
$page_url = $PHP_SELF . "?page=".$page_next;
$arr["page_next"] = "|<a href=\"".$page_url."\">下一页</a>\n";
}
$arr["all"] = "<font color=\"#FF0000\">".$page ."</font>/". $pagecount . "页\n";
$arr["first"] = "<a href=\"".$PHP_SELF."?page=1\">首页</a>\n|";
$arr["last"] = "|<a href=\"".$PHP_SELF."?page=".$pagecount."\">末页</a>\n";
$plfront="";
if($page<=5 && $page>=1){
for($i=1;$i<=9;$i++){
$plfront.= " <a href=\"".$PHP_SELF."?page=$i\">".$i."</a>";
}
}elseif($page>5 && $page<$pagecount-5){
for($i=$page-4;$i<$page+5;$i++){
$plfront.= " <a href=\"".$PHP_SELF."?page=$i\">".$i."</a>";
}
}else{
for($i=$pagecount-8;$i<=$pagecount;$i++){
$plfront.= " <a href=\"".$PHP_SELF."?page=$i\">".$i."</a>";
}
}
$arr["pagelist"] = $plfront." ";
$arr["nums"] = $num;
}