//每页记录
$pageSize=2;
//得到当前页
$page=$_REQUEST['page'] ? (int)$_REQUEST['page'] : 1;
//按分页查询数据
$rows=getAdminByPage($page,$pageSize);
//分页查询数据
function getAdminByPage($page,$pageSize){
$sql="select * from admin";
global $totalRows;
$totalRows=getResultNum($sql);
global $totalPage;
$totalPage=ceil($totalRows/$pageSize);
if($page<1||$page==null||!is_numeric($page)){
$page=1;
}
if($page>=$totalPage){
$page=$totalPage;
}
$offset=($page-1)*$pageSize;
$sql="select * from admin limit {$offset},{$pageSize}";
$rows=fetchAll($sql);
return $rows;
}
//分页函数
function showPage($page,$totalPage,$where=null,$sep=" "){ $where=($where==null)? null:"&".$where; $url = $_SERVER['PHP_SELF']; $index = ($page == 1) ? "首页" : "<a href='{$url}?page=1{$where}'>首页</a>"; $last = ($page == $totalPage) ? "尾页" : "<a href='{$url}?page={$totalPage}{$where}'>尾页</a>"; $prevPage=($page>=1)?$page-1:1; $nextPage=($page>=$totalPage)?$totalPage:$page+1; $prve = ($page == 1) ? "上一页" : "<a href='{$url}?page= {$prevPage}{$where}'>上一页</a>"; $next = ($page == $totalPage) ? "下一页" : "<a href='{$url}?page={$nextPage}{$where}'>下一页</a>"; $str = "总共{$totalPage}页/当前是第{$page}页"; for ($i = 1; $i <= $totalPage; $i++) { if ($page == $i) { $p .= "[{$i}]"; } else { $p .= "<a href='{$url}?page={$i}{$where}'>[{$i}]</a>"; } } $pageStr = $str .$sep . $index .$sep . $prve .$sep . $p .$sep . $next .$sep . $last; return $pageStr;}
//显示分页
<?php if($totalRows>$pageSize){?>
<tr> <td colspan="4"> <?php echo showPage($page,$totalPage)?></td></tr>
<?php }?>