<?php
$size = 5;
$p = $_GET['p']?$_GET['p'] : 1;
//链接数据库
$link = mysqli_connect("127.0.0.1","root","root","exam");
$sql = "select * from exam";
$res = mysqli_query($link,$sql);
//var_dump($res);die;
//获取条数
$pagSum = mysqli_num_rows($res);
//print_r($pagSum);
//计算偏移量
$limit = ($p-1)*$size;
//计算总页数ceil(总条数/每页显示条数)
$sumSize = ceil($pagSum/$size);
//截取内容
$sql="select * from exam limit $limit,$size";
$res = mysqli_query($link,$sql);
$data = mysqli_fetch_all($res,1);
$prev = $p - 1 < 1 ? 1: $p - 1;
$next = $p + 1 > $sumSize ? $sumSize: $p + 1;
?>
RUNOOB.COM
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 实例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>带边框表格</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>序号</th>
<th>标题</th>
<th>单价</th>
<th>库存</th>
</tr>
</thead>
<tbody>
//循环展示数据
<?php
foreach($data as $val){
?>
<tr>
<td> <?php echo $val['id']?> </td>
<td> <?php echo $val['title']?> </td>
<td> <?php echo $val['price']?> </td>
<td> <?php echo $val['num']?> </td>
</tr>
<?php
}
?>
</tbody>
</table>
<ul class="pagination">
<li class="page-item"><a class="page-link" href="show.php?p=<?php echo $prev ?> ">上一页</a></li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item"><a class="page-link" href="show.php?p=<?php echo $next?> ">下一页</a></li>
</ul>
</div>
</html>
|