PHP页面分页函数

 很久前些的一些文章,那时候技术还在初级阶段,在现在看来代码够烂的,但是人总是一步步走上来的,希望能给初学者一点鼓励了!

<?php
/*********************************************************
 filename:multi.php
 describe: 显示多于一页的链接
 function:分页显示模块
 author:  etongchina@gmail.com
 checked: 2008-02-15 18:05
 调用方式:
$num:数据库总记录数
$perpage:每页显示条数
$curr_page:当前页码
$mpurl:链接的URL
$type:url改写后的链接
默认规则为:当前页码.htm
*********************************************************/

function multi($num, $perpage, $curr_page, $mpurl,$type = 0) {
 $multipage = '';
 if(!is_numeric($curr_page) || ( $curr_page < 1 )){
  showmessage("无效页面参数!");
  //自定义的输出提示函数
 }
 if($type == 1){
  $script = '';
 }else{
  $script = ".htm";
 }
 if($num > $perpage) {
  $page = 6;
  $offset = 2;
  $pages = ceil($num / $perpage);
  if($curr_page > $pages){
   showmessage("您查看的页面不存在!");
  }
  $from = $curr_page - $offset;
  $to = $curr_page + $page - $offset - 1;
  if($page > $pages) {
   $from = 1; $to = $pages;
  }else{
   if($from < 1) {
    $to = $curr_page + 1 - $from;
    $from = 1;
    if(($to - $from) < $page && ($to - $from) < $pages) {
     $to = $page;
    }
   }elseif($to > $pages) {
    $from = $curr_page - $pages + $to;
    $to = $pages;
    if(($to - $from) < $page && ($to - $from) < $pages) {
     $from = $pages - $page + 1;
    }
   }
  }
  $start = ($curr_page-1)*$perpage+1;
  if($curr_page == $pages){
  $other = ($num%$perpage)?($num%$perpage):$perpage;
  $end = ($curr_page-1)*$perpage+$other;
  }else{
  $end = $curr_page*$perpage;
  }
  $multipage .='<table border="0" cellspacing="1" cellpadding="3"><form name="multi">';
  $multipage .='<tr><td>【第<font color="red">'.$start.'</font>-<font color="red">'.$end.'</font>条,共'.$num.'条】 页码:<b>'.$curr_page.'</b> [共 <b>'.$pages.'</b> 页]:</td>';
  $multipage .= ($curr_page - 1 > $offset)?'<td width="30"  align=center><a href="'.$mpurl.'1'.$script.'" title="首页">首页</a></td>':'';

  $multipage .= ($curr_page > 1)?'<td width="20"  align=center><a href="'.$mpurl.($curr_page - 1).$script.'" title="上一页"><<</a></td>':"";
  for($i = $from; $i <= $to; $i++) {
   if($i != $curr_page) {
    $multipage .= '<td width="8"  align=center><a href="'.$mpurl.$i.$script.'">'.$i.'</a></td>';
   } else {
    $multipage .= '<td width="8"  align=center><font color=red><u><b>'.$i.'</b></u></font></td>';
   }
  }
  if($pages > $curr_page){
  $multipage .= '<td width="20" align=center><a href="'.$mpurl.($curr_page + 1).$script.'" title="下一页"> >></a></td>';
  
  }
  if($pages - $curr_page + $offset >= $page){
  $multipage .= '<td align=center width="30"><a href="'.$mpurl.$pages.$script.'" title="尾页">尾页</a></td>';
  }
  $multipage .='<td><input style="WIDTH: 30px; HEIGHT: 16px" value=1 name=page>
                </font>页<input type="button" value="GO" style="height:22px;line-height:18px" οnclick="javascript:gotourl(/''.$mpurl.'/')"></td>';
  $multipage .= "</tr></form></table>";
  $multipage .= "<script>function gotourl(inurl){ add = multi.page.value+'".$script."';inurl = inurl+add;location.href=inurl; }</script>";
  
  
 }
 return $multipage;
}


?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值