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