您的位置:首页 > 编程语言 > PHP开发

使用php完成一个用户注册以及管理的demo(三)

2015-02-13 16:17 627 查看
(1)先制作一个页面,完成显示列表的操作,还没有进行分页。

alertMes是自己封装的一个函数,源码如下

function alertMes($mes,$url)

{

echo "<script>alert('$mes');</script>";

echo "<script>window.location='{$url}';</script>";

}


<?php
require_once '../include.php';
$sql="select id,username,email,activeFlag from imooc_user";
$rows=fetchAll($sql);
if(!$rows)
{
alertMes("没有用员,请添加!","addUser.php");
exit;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<table class="table" cellspacing="1" cellpadding="1" border="1">
<thead>
<tr>
<th width="15%">编号</th>
<th width="20%">用户名称</th>
<th width="20%">用户邮箱</th>
<th width="20%">是否激活</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php $i=1;foreach($rows as $row):?>
<tr>
<!--这里的id和for里面的c1 需要循环出来-->
<td><input type="checkbox" id="c1" class="check"><label for="c1" class="label"><?php echo $i;?></label></td>
<td><?php echo $row['username'];?></td>
<td><?php echo $row['email'];?></td>
<td align="center"><?php echo $row['activeFlag'];?></td>
<td align="center"><input type="button" value="修改" class="btn" onclick="editAdmin(<?php echo $row['id'];?>)"><input type="button" value="删除" onclick="delAdmin(<?php echo $row['id'];?>)"></td>
</tr>
<?php $i++;endforeach;?>
</tbody>

</table>
</body>
</html>


效果如图所示


接下来讲解分页效果的实现

方法,当用户点击页数的时候,将页数信息通过get方式发送出去。在制作分页的时候,我们需要知道的有以下一些信息。每页需要展示的条数,计算下来一共有多少页,从而确定首页是什么尾页是什么。

//得到记录的条数
$sql="select * from imooc_admin";
$totalRows=getResultNum($sql);
//定义每页显示的数量,并确定总页数
$pageSize=2;
$totalPage=ceil($totalRows/$pageSize);
//得到当前页是多少,并对边界条件进行判断(防止用户在导航栏恶意输入)
$page=$_REQUEST['page']?(int)$_REQUEST['page']:1;
if($page<1||$page==null||!is_numeric($page)){
$page=1;
}
if($page>=$totalPage)$page=$totalPage;
//从多少条之后再加$page条,取得结果集
$offset=($page-1)*$pageSize;
$sql="select id,username,email,activeFlag from imooc_user limit {$offset},{$pageSize}";
$rows=fetchAll($sql);
//无记录则跳转
if(!$rows)
{
alertMes("没有用员,请添加!","addUser.php");
exit;
}
下面封装一个函数根据总页数和当前页数将所需的分页链接制作出来
在page.func.php页面中
$where是为了让使用者想要传递其它信息时使用,$sep则代表的是间距。
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>";
$prev=($page==1)?"上一页":"<a href='{$url}?page=".($page-1)."{$where}'}'>上一页</a>";
$next=($page==$totalPage)?"下一页":"<a href='{$url}?page=".($page+1)."{$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.$index.$sep.$prev.$sep.$p.$sep.$next.$sep.$last;
return $pageStr;
}


经过上面的引入,我们最终的代码如下

<?php
require_once '../include.php';
$sql="select id,username,email,activeFlag from imooc_user";
$totalRows=getResultNum($sql);
//定义每页显示的数量,并确定总页数
$pageSize=2;
$totalPage=ceil($totalRows/$pageSize);
//得到当前页是多少,并对边界条件进行判断(防止用户在导航栏恶意输入)
$page=$_REQUEST['page']?(int)$_REQUEST['page']:1;
if($page<1||$page==null||!is_numeric($page)){
$page=1;
}
if($page>=$totalPage)$page=$totalPage;
//从多少条之后再加$page条
$offset=($page-1)*$pageSize;
$sql="select id,username,email,activeFlag from imooc_user limit {$offset},{$pageSize}";
$rows=fetchAll($sql);
if(!$rows)
{
alertMes("没有用员,请添加!","addUser.php");
exit;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<table class="table" cellspacing="1" cellpadding="1" border="1">
<thead>
<tr>
<th width="15%">编号</th>
<th width="20%">用户名称</th>
<th width="20%">用户邮箱</th>
<th width="20%">是否激活</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php $i=1;foreach($rows as $row):?>
<tr>
<!--这里的id和for里面的c1 需要循环出来-->
<td><input type="checkbox" id="c1" class="check"><label for="c1" class="label"><?php echo $i;?></label></td>
<td><?php echo $row['username'];?></td>
<td><?php echo $row['email'];?></td>
<td align="center"><?php echo $row['activeFlag'];?></td>
<td align="center"><input type="button" value="修改" class="btn" onclick="editAdmin(<?php echo $row['id'];?>)"><input type="button" value="删除" onclick="delAdmin(<?php echo $row['id'];?>)"></td>
</tr>
<?php $i++;endforeach;?>
<?php if($rows>$pageSize):?>
<TR>
<TD colspan="5"><?php echo showPage($page, $totalPage)?></TD>
</TR>
<?php endif;?>
</tbody>
</table>
</body>
</html>


就到这里了,下一期主要讲解用户修改和删除操作.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐