PHP实现分页功能(原生PHP代码+bootstrap)

PHP实现分页功能(原生PHP代码+bootstrap

开发说明

这是接着我上一个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>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值