越简单越好!

php 分页函数

发表于 2006-12-13 11:19 | 1037次阅读 0次点赞   PHP

<?php
function page($sql,$pagenum){
 global $cur_page,$total_page_nums,$last_page,$next_page; 
 $result =mysql_query($sql);
 $num=mysql_num_rows($result);  
 $total_page_nums=intval($num/$pagenum);
 if($num%$pagenum)
 $total_page_nums = $total_page_nums + 1;
 if($_POST[go]){
  $cur_page=$_POST[curpage];
  if($cur_page>$total_page_nums){
   $cur_page=$total_page_nums;
  }
 }else{
  $cur_page=$_GET[cur_page];
  if($cur_page>$total_page_nums){
   $cur_page=$total_page_nums;
  }
 }
 if(!isset($cur_page))
 $cur_page=1;
 $last_page=$cur_page-1;
 if($last_page<1)
 $last_page=1;
 $next_page=$cur_page+1;
 if($next_page>$total_page_nums)
 $next_page=$total_page_nums;
 $pages=($cur_page*$pagenum)-$pagenum;
 $sql.=" limit $pages,$pagenum";
 return $sql;
}
function disp()
{
global $cur_page,$total_page_nums,$last_page,$next_page;
/** 分析URL **/
$url=$_SERVER["REQUEST_URI"];
if(isset($_GET['cur_page'])){
 $cur_page = $_GET['cur_page'];
}else{
 $cur_page=1;
}
$parse_url=parse_url($url);
$url_query=$parse_url["query"]; 
if($url_query){

 $url_query=ereg_replace("(^|&)cur_page=$cur_page","",$url_query);

 $url=str_replace($parse_url["query"],$url_query,$url);

 if($url_query) $url.="&cur_page"; else $url.="cur_page";
}else {
 $url.="?cur_page";
}
/** 生成URL **/

 $pageinfo="<table width="500" border="0" align="center" cellpadding="0" cellspacing="0" ><tbody>";
 $pageinfo.="<tr><td height="25">";
 $control .="共<font color='#ff9900'>".$total_page_nums."</font>页 ";
 $control .="<a href='$url=1'>首页</a>| ";
 $control .="<a href='$url=$last_page'>上页</a>| ";
 $control .="<a href='$url=$next_page'>下页</a>| ";
 $control .="<a href='$url=$total_page_nums'>末页</a> ";
 $pageinfo.="<form action="$PHP_SELF" method="post">".$control."跳至第<input type="text" name="curpage" size="1" value="".$cur_page."" id="pageid" >页<input type="submit" name="go" class="btn" value="go"></form>";
 $pageinfo.="</td></tr></table>";
 return $pageinfo;
}
?>











返回顶部 ^