开发说明
这是接着我上一个ajax获取表单提交内容博文,在原来的页面增加分页功能!分页的样式是用bootstrap。废话不多说直接上代码
图片
代码
<script src="js/bootstrap.min.js"></script>
<body>
<?php
// 1.连接
$link = @mysqli_connect('localhost','user','password'); // 返回 boolean / object
// 3.选库
$res = mysqli_select_db($link , 'user');
// 4.设置字符集
$res = mysqli_set_charset($link , 'utf8');
// 5.准备sql
$num_rec_per_page=10; // 每页显示数据条数
@$page = $_GET["page"]?$_GET["page"]:$page=1; //获取要显示的页数
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM user LIMIT $start_from, $num_rec_per_page"; //根据条件查询数据库
// 6.确认发送
$result = mysqli_query($link , $sql); // 返回 boolean / object(又称结果集)
$userlist = [];
while( $row = mysqli_fetch_assoc($result) ){
$userlist[] = $row;
}
//查询分页功能要的数据
$count_sql = "SELECT * FROM user"; //数据表总数量
$rs_result = mysqli_query($link ,$count_sql); //查询数据
$total_records = mysqli_num_rows($rs_result); // 总共的记录条数
$total_pages = ceil($total_records / $num_rec_per_page); // 总页数
// 8.释放资源
mysqli_free_result($result);
// 9.关闭连接
$res = mysqli_close( $link );
?>
<div style="margin:0 auto;width:100%">
<div class="col-md-6" style="margin:0 auto;width:100%">
<table class="table table-hover table-striped" style="margin:0 auto;width:800px" >
<tr >
</tr>
<tr>
<td colspan="6"><h4 style="float:left;"><a href="useradd.php">新增</a></h4>
</td>
</tr>
<tr >
<td>id</td>
<td>用户名</td>
<td>注册时间</td>
<td>过期时间</td>
<td>操作</td>
</tr>
<?php
foreach($userlist as $list){
echo '<form class="form-inline" action="userupdate.php" method="post" >';
echo "<input type='text' style='display:none;' name='id' value=".$list['id'].">";
echo ' <tr><td>'.$list['id'].'</td>';
echo '<td>'.$list['username'].'</td>';
echo '<td>'.date("Y-m-d",$list['createtime']).'</td>';
echo '<td>'.date("Y-m-d",$list['createtime']+$list['indate']*86400).'</td>';
echo '<td style="float:left;"><button type="submit" class="btn btn-info">修改</button></form>';
echo '<form style="float:right;margin-left: 20px;" class="form-inline" action="" method="post" οnclick="return clearFn(this)">';
echo "<input type='text' style='display:none;' name='id' value=".$list['id'].">";
echo '<button type="button" class="btn btn-warning selectbt" οnclick="return clearFn(this)">重置密码</button></form>';
echo '<form style="float:right;margin-left: 20px;" class="form-inline" action="" οnclick="return deleteFn(this)">';
echo "<input type='text' style='display:none;' name='id' value=".$list['id'].">";
echo '<button type="button" class="btn btn-danger deletebt" οnclick="return deleteFn(this)">删除</button></tr></td></form>';
}
?>
</table>
//分页功能
<nav aria-label="Page navigation" style="width: 800px;margin: 100px auto;text-align: center;">
<ul class="pagination">
<li>
<a href="user.php?page=1" aria-label="Previous">
<span aria-hidden="true"></span>
</a>
</li>
<li><?php for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='user.php?page=".$i."'>".$i."</a> ";
}; ?></li>
<li>
<a href="user.php?page=<?php echo $total_pages ?>" aria-label="Next">
<span aria-hidden="true"></span>
</a>
</li>
</ul>
<div><?php echo "总共".$total_records."条记录" ?></div>
</nav>
</div>
</div>
</body>