php7+layui超简单分页
本帖最后由 arman415 于 2023-4-7 15:45 编辑话不多说直接先看效果
代码如下:
<?php
try {
$dsn = "mysql:host=localhost;dbname=数据库名";
$username = "数据库名";
$password = "数据库密码";
$pdo = new PDO($dsn, $username, $password);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
//计算总记录数
$total=$pdo->query('SELECT COUNT(1) FROM `表名`')->fetchColumn();
//每页显示记录数
$pageSize=10;
//计算总页数
$pageCount=ceil($total/$pageSize);
//当前页码,默认为第一页
$pageNum=isset($_GET['page']) ? $_GET['page'] : 1;
//起始记录数
$start=($pageNum-1)*$pageSize;
//查询分页数据
$sql="SELECT * FROM `表名` LIMIT {$start},{$pageSize}";
$stmt=$pdo->query($sql);
$list=$stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP+Layui分页</title>
<link rel="stylesheet" >
</head>
<body>
<div class="layui-container">
<br>
<br>
<center>
<h3>超简单php+layui分页</h3>
</center>
<table class="layui-table">
<thead>
<tr>
<th>ID</th>
<th>产品名</th>
<th>产品库存</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach($list as $vo){ ?>
<tr>
<td><?php echo $vo['id']; ?></td>
<td>产品<?php echo $vo['id']; ?></td>
<td>库存<?php echo $vo['id']; ?></td>
<td><a href=delete.php?id=<?php echo $vo['id']; ?></a>删除</td>
</tr>
<?php } ?>
</tbody>
</table>
<div id="demo"></div>
</div>
<script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script>
<script>
layui.use(['laypage'], function(){
var laypage = layui.laypage;
laypage.render({
elem: 'demo',
count: <?php echo $total; ?>,
limit: <?php echo $pageSize; ?>,
curr: <?php echo $pageNum; ?>,
jump: function(obj,first){
if(!first){
window.location.href = '?page='+obj.curr;
}
}
});
});
</script>
</body>
</html> 添加一个搜索:lol
<?php
try {
$dsn = "mysql:host=localhost;dbname=数据库名";
$username = "数据库名";
$password = "数据库密码";
$pdo = new PDO($dsn, $username, $password);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
// 处理搜索关键字
$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : '';
// 计算总记录数
$sql = "SELECT COUNT(1) FROM `表名`" . ($keyword ? " WHERE `产品名` LIKE '%$keyword%'" : "");
$total = $pdo->query($sql)->fetchColumn();
// 每页显示记录数
$pageSize = 10;
// 计算总页数
$pageCount = ceil($total/$pageSize);
// 当前页码,默认为第一页
$pageNum = isset($_GET['page']) ? $_GET['page'] : 1;
// 起始记录数
$start = ($pageNum - 1) * $pageSize;
// 查询分页数据
$sql = "SELECT * FROM `表名`" . ($keyword ? " WHERE `产品名` LIKE '%$keyword%'" : "") . " LIMIT {$start},{$pageSize}";
$stmt = $pdo->query($sql);
$list = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PHP+Layui分页+搜索</title>
<link rel="stylesheet" >
</head>
<body>
<div class="layui-container">
<br>
<form class="layui-form" action="" method="get">
<div class="layui-inline">
<input type="text" name="keyword" placeholder="请输入产品名" value="<?php echo htmlspecialchars($keyword); ?>" class="layui-input">
</div>
<button class="layui-btn" lay-submit lay-filter="search"><i class="layui-icon layui-icon-search"></i></button>
<a href="?page=1" class="layui-btn layui-btn-primary">重置</a>
</form>
<br>
<table class="layui-table">
<thead>
<tr>
<th>ID</th>
<th>产品名</th>
<th>产品库存</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach($list as $vo){ ?>
<tr>
<td><?php echo $vo['id']; ?></td>
<td><?php echo $vo['产品名']; ?></td>
<td><?php echo $vo['产品库存']; ?></td>
<td><a href="delete.php?id=<?php echo $vo['id']; ?>">删除</a></td>
</tr>
<?php } ?>
</tbody>
</table>
<div id="demo"></div>
</div>
<script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script>
<script>
layui.use(['laypage', 'form'], function(){
var laypage = layui.laypage;
var form = layui.form;
// 渲染分页导航栏
laypage.render({
elem: 'demo',
count: <?php echo $total; ?>,
limit: <?php echo $pageSize; ?>,
curr: <?php echo $pageNum; ?>,
jump: function(obj, first){
if(!first){
// 点击页码时,重新加载页面
window.location.href = '?page=' + obj.curr + '&keyword=<?php echo urlencode($keyword); ?>';
}
}
});
// 监听搜索表单提交事件
form.on('submit(search)', function(data){
window.location.href = '?keyword=' + data.field.keyword;
return false;
});
});
</script>
</body>
</html> 基础知识,适合我 哈哈哈哈哈哈 刚培训班出来吧~ 💪🏻加油!! zhq114 发表于 2023-3-14 11:36
刚培训班出来吧~ 💪🏻加油!!
自学的,去年论坛开放注册的时候没有发布文章就把我清了,今年害怕有被清。{:1_886:} limit 数据多的时候咋办,limit 100000000,10😂 楼主,建议limit和offset都做数量控制,避免别人整一个10000000,可能数据库直接就查崩溃了 lovemit 发表于 2023-3-14 12:29
limit 数据多的时候咋办,limit 100000000,10😂
默认情况$pageSize=10;查询十条数据 xavier001 发表于 2023-3-14 16:18
楼主,建议limit和offset都做数量控制,避免别人整一个10000000,可能数据库直接就查崩溃了
默认情况$pageSize=10;查询十条数据 加油加油! arman415 发表于 2023-3-14 16:35
默认情况$pageSize=10;查询十条数据
每次查询10条,开始条数100000000的话,全表查不慢死了…
页:
[1]
2