PHP分页函数

Standard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
if(!function_exists(pageft)){
function pageft($totle,$displaypg=20,$url=''){
global $page,$firstcount,$pagenav,$_SERVER;
$GLOBALS["displaypg"]=$displaypg;
if(!$page) { $page=1; }
if(!$url){
$url=$_SERVER["REQUEST_URI"];
}
//URL分析:
$parse_url=parse_url($url);
$url_query=$parse_url["query"]; //单独取出URL的查询字串
if($url_query){
$url_query=ereg_replace("(^|&)page=$page","",$url_query);
//将处理后的URL的查询字串替换原来的URL的查询字串:
$url=str_replace($parse_url["query"],$url_query,$url);
//在URL后加page查询信息,但待赋值:
if($url_query){
$url.="&page";
}else{
$url.="page";
}
}else {
$url.="?page";
}
$lastpg=ceil($totle/$displaypg); //最后页,也是总页数
$page=min($lastpg,$page);
$prepg=$page-1; //上一页
$nextpg=($page==$lastpg ? 0 : $page+1); //下一页
$firstcount=($page-1)*$displaypg;
//开始分页导航条代码:
$pagenav="显示第 <B>".($totle?($firstcount+1):0)."</B>-<B>".min($firstcount+$displaypg,$totle)."</B> 条记录,共 $totle 条记录<BR>";
//如果只有一页则跳出函数:
if($lastpg<=1) return false;
$pagenav.=" <a href='$url=1'>首页</a> ";
if($prepg){
$pagenav.=" <a href='$url=$prepg'>前页</a> ";
}else{
$pagenav.=" 前页 ";
}
if($nextpg){
$pagenav.=" <a href='$url=$nextpg'>后页</a> ";
}else{
$pagenav.=" 后页 ";
}
$pagenav.=" <a href='$url=$lastpg'>尾页</a> ";
//下拉跳转列表,循环列出所有页码:
$pagenav.=" 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for($i=1;$i<=$lastpg;$i++){
if($i==$page){
$pagenav.="<option value='$i' selected>$i</option>\n";
}else{
$pagenav.="<option value='$i'>$i</option>\n";
}
}
$pagenav.="</select> 页,共 $lastpg 页";
}
}
 
$sql="SELECT * FROM tb";
//取得总信息数
$result=mysql_query($sql);
$total=mysql_num_rows($result);
//调用pageft(),每页显示10条信息(使用默认的20时,可以省略此参数),使用本页URL(默认,所以省略掉)。
pageft($total,10,"http://www.yemaosheng.com/index");
//现在产生的全局变量就派上用场了:
$result=mysql_query($sql." limit $firstcount,$displaypg");
$i=1;
while($row=mysql_fetch_array($result)){
$array[]=array("index"=>$i,"date"=>$row['date'],"type"=>$row['type'],"value"=>$row['value']);
$i++;
}
$pagenav;//分页导航条代码
$main_smarty->assign('mainlist', $array);
$main_smarty->assign('pagenum', $pagenav);
$main_smarty->display('x.tpl');
 
----------------smarty template x.tpl----------------
<table>
{section name=sec1 loop=$mainlist}
<tr>
<td width="76"><strong>{$mainlist[sec1].index}</strong></td>
<td width="138">{$mainlist[sec1].date}</td>
<td width="274">{$mainlist[sec1].type}</td>
<td width="114">{$mainlist[sec1].value}</td>
</tr>
{/section}
</table>
<center>{$pagenum}</center>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.