<?php
/*
使用方法:参数1:当前页数,参数2:每页显示数据条数,参数三:分页sql;参数四:查询当前条件下记录总数sql,参数5:表单提交或网页获取的参数,字符串形式,默认为空
*/
class page{
//动态设置分页数目
public function index($page,$page_size,$sql,$sql_total,$param_str=”"){
$off=($page-1)*$page_size;
$off_size=$page*$page_size;
$total_all=mysql_num_rows(mysql_query($sql_total));
$page_count=ceil($total_all/$page_size);
$param_str=trim($param_str);//去掉参数两端空格
if($page<=$page_count&&$page+5<=$page_count&&$page-4>0){
$total = $page+5;
$n = $page-4;
}elseif($page<=$page_count&&$page+5>$page_count&&$page-4>0){//往后自动翻页超出界限
$total = $page_count;
if($total-9>0){
$n = $total-9;
}else{
$n = 1;
}
}elseif($page<=$page_count&&$page+5>$page_count&&$page-4<=0){//往后自动翻页超出界限
$total = $page_count;
$n = 1;
}elseif($page<=$page_count&&$page+5<=$page_count&&$page-10<=0&&$page_count>=10){//往前自动翻页超出界限
$total = 10;
$n = 1;
}elseif($page<=$page_count&&$page+5<=$page_count&&$page-10<=0&&$page_count<=10){//往前自动翻页超出界限
$total = $page_count;
$n = 1;
}elseif($page_count==”1″){
$total = 1;
$n = 1;
}elseif($page==1&&$page<$page_count){
$total = 10;
$n = 1;
}
//动态设置分页数目完毕
foreach ($_GET as $key=>$value){
if(“$key”!=”page”){
$param[$key] = $value;
}
}
if(count($param)){
foreach ($param as $key=>$value){
$str.=$key.”=”.$value.”&”;
}
$str=substr($str,0,-1);
$url = “http://”.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].”?”.$str;
}elseif($param_str){
$url = “http://”.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].”?”.$param_str;
}else{
$url = “http://”.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
}
//分页开始
if($str){//如果url带参数
$page_arr[]=”<a href=’”.$_SERVER['SCRIPT_NAME'].”?$str’>首页</a>”;
if($page>1)$page_arr[] =”<a href=’$url&page=”.($page-1).”‘>上一页</a>”;
if($page_count>1){
for($n;$n<=$total;$n++){
if($page==$n){
$page_arr[] = “<a href=’$url&page=$n’><span id=’link_background’>$n </span></a>”;
}else{
$page_arr[] = “<a href=’$url&page=$n’>[$n] </a>”;
}
}
}
if($page<$page_count&&$page_count>1)$page_arr[] =”<a href=’$url’>下一页</a>”;
}elseif($param_str){//如果有提交的参数
$page_arr[]=”<a href=’”.$_SERVER['SCRIPT_NAME'].”‘>首页</a>”;
if($page>1)$page_arr[] =”<a href=’$url&page=”.($page-1).”‘>上一页</a>”;
if($page_count>1){
for($n;$n<=$total;$n++){
if($page==$n){
$page_arr[] = “<a href=’$url&page=$n’><span id=’link_background’>$n </span></a>”;
}else{
$page_arr[] = “<a href=’$url&page=$n’>[$n] </a>”;
}
}
}
if($page<$page_count&&$page_count>1)$page_arr[] =”<a href=’$url&pag
···
···