我的分页封装后,点击上下一页,首尾都不管用啊!
分页:
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;
$prev=($page==1)?"上一页":"<a href='{$url}?page={$prevPage}{$where}'>上一页</a>";
//下一页
$next=($page==$totalPage)?"下一页":"<a href='{$url}?page={$nextPage}{$where}'>下一页</a>";
$str="总共{$pagePage}页/当前是第{$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. $prev.$sep . $p.$sep . $next.$sep . $last;
return $pageStr;
}
admin.inc.php
*分页
*/
function getAdminByPage($page,$pageSize=2){
$sql="select * from sam_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 sam_admin limit {$offset},{$pageSize}";
$rows=fetchAll($sql);
return $rows;
}
listAdmin
$pageSize=2;
$rows = getAdminByPage($page,$pageSize);
$page=$_REQUEST['page']?(int)$_REQUEST['page']:1;
//$rows = getAllAdmin();
if(!$rows){
alertMes("没有管理员,请添加!","addAdmin.php");
exit;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>-.-</title>
<link rel="stylesheet" href="styles/backstage.css">
</head>
<body>
<div class="details">
<div class="details_operation clearfix">
<div class="bui_select">
<input type="button" value="添 加" class="add" onclick="addAdmin()">
</div>
</div>
<!--表格-->
<table class="table" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th width="15%">编号</th>
<th width="30%">管理员名称</th>
<th width="30%">邮箱</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach($rows as $row):?>
<tr>
<!--这里的id和for里面的c1 需要循环出来-->
<td><input type="checkbox" id="c1" class="check"><label for="c1" class="label"><?php echo $row['id'];?></label></td>
<td><?php echo $row['username'];?></td>
<td><?php echo $row['email'];?></td>
<td align="center">
<input type="button" value="修改" class="btn" onclick="editAdmin(<?php echo $row['id'];?>)">
<input type="button" value="删除" class="btn" onclick="delAdmin(<?php echo $row['id'];?>)">
</td>
</tr>
<?php endforeach;?>
<?php if($totalRows>$pageSize):?>
<tr>
<td colspan="4"><?php echo showPage($page,$totalPage);?></td>
</tr>
<?php endif;?>
</tbody>
</table>
</div>
</body>
</html>
<script type="text/javascript">
function editAdmin(id){
window.location="editAdmin.php?id="+id;
}
function addAdmin(){
window.location="addAdmin.php";
}
function delAdmin(id){
if(window.confirm("您确定要删除吗?删除之后不可以恢复哦!")){
window.location="doAdminAction.php?act=delAdmin&id="+id;
}
}
</script>