当前位置 : 主页 > 网页制作 > Bootstarp >

Bootstrap百度分页

来源:互联网 收集:自由互联 发布时间:2021-06-12
? php $pdo = new PDO(‘mysql:host=127.0.0.1;dbname=1703a‘,‘root‘,‘root‘ ); // 接收当前页 $page = empty ( $_GET [‘page‘]) ? 1 : $_GET [‘page‘ ]; // 查询总条数 $count_sql = "select count(username) from user" ; $re
<?php
$pdo = new PDO(‘mysql:host=127.0.0.1;dbname=1703a‘,‘root‘,‘root‘);
//接收当前页
$page = empty($_GET[‘page‘]) ? 1 : $_GET[‘page‘];
//查询总条数
$count_sql = "select count(username) from user";
$res = $pdo->query($count_sql)->fetch(PDO::FETCH_ASSOC);
$count = $res[‘count(username)‘];
//每页显示的条数
$length = 20;
//总页数
$num_page = ceil($count/$length);
//偏移量
$limit = ($page-1)*$length;
//查询
$sql = "select * from user limit $limit,$length";
$data = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
?>

<link rel="stylesheet" type="text/css" href="bootstrap4.min.css">

<table class=‘table‘>
    <tr>
        <th>主键</th>
        <th>账号</th>
        <th>昵称</th>
        <th>个性签名</th>
        <th>注册时间</th>
        <th>操作</th>
    </tr>

    <?php foreach($data as $k => $v){ ?>
        <tr>
            <td><?php echo $v[‘id‘];?></td>
            <td><?php echo $v[‘username‘];?></td>
            <td><?php echo $v[‘nickname‘];?></td>
            <td><?php echo $v[‘fname‘];?></td>
            <td><?php echo date(‘Y-m-d H:i:s‘,$v[‘create_at‘]);?></td>
            <td>
                <a href="">删除</a>
                <a href="">修改</a>
            </td>
        </tr>
    <?php } ?>
</table>

<nav aria-label="...">
  <ul class="pagination">
    <li class="page-item">
      <a class="page-link" href="list.php?page=1">首页</a>
    </li>
    
    <?php
        if($page>=5){
            for($i=$page-4;$i<$page;$i++){
                echo "<li class=\"page-item\"><a class=\"page-link\" href=\"list.php?page=".$i."\">".$i."</a></li>";
            }
        }else{
            for($i=1;$i<$page;$i++){
                echo "<li class=\"page-item\"><a class=\"page-link\" href=\"list.php?page=".$i."\">".$i."</a></li>";
            }
        }
        
    ?>

    <li class="page-item active">
      <a class="page-link" href="list.php?page=<?php echo $page;?>"><?php echo $page;?> <span class="sr-only">(current)</span></a>
    </li>
    
    <?php
        for($i=$page+1;$i<=$page+4;$i++){
            echo "<li class=‘page-item‘><a class=‘page-link‘ href=‘list.php?page=".$i."‘>".$i."</a></li>";
        }
    ?>

    <li class="page-item">
      <a class="page-link" href="list.php?page=<?php echo $num_page;?>">尾页</a>
    </li>
  </ul>
</nav>
网友评论