php7+layui超简单分页示例代码

 1 <?php
 2 try {
 3   $dsn = "mysql:host=localhost;dbname=数据库名";
 4   $username = "数据库名";
 5   $password = "数据库密码";
 6   $pdo = new PDO($dsn, $username, $password);
 7 } catch(PDOException $e) {
 8   echo "Connection failed: " . $e->getMessage();
 9 }
10   
11 //计算总记录数
12 $total=$pdo->query('SELECT COUNT(1) FROM `表名`')->fetchColumn();
13 //每页显示记录数
14 $pageSize=10;
15 //计算总页数
16 $pageCount=ceil($total/$pageSize);
17 //当前页码,默认为第一页
18 $pageNum=isset($_GET['page']) ? $_GET['page'] : 1;
19 //起始记录数
20 $start=($pageNum-1)*$pageSize;
21 //查询分页数据
22 $sql="SELECT * FROM `表名` LIMIT {$start},{$pageSize}";
23 $stmt=$pdo->query($sql);
24 $list=$stmt->fetchAll(PDO::FETCH_ASSOC);
25 ?>
26   
27 <!DOCTYPE html>
28 <html>
29 <head>
30   <meta charset="utf-8">
31   <title>PHP+Layui分页</title>
32 <link rel="stylesheet" >
33 </head>
34 <body>
35 <div class="layui-container">
36     <br>
37     <br>
38     <center>
39     <h3>超简单php+layui分页</h3>
40     </center>
41   <table class="layui-table">
42     <thead>
43     <tr>
44       <th>ID</th>
45       <th>产品名</th>
46       <th>产品库存</th>
47       <th>操作</th>
48     </tr>
49     </thead>
50     <tbody>
51     <?php foreach($list as $vo){ ?>
52       <tr>
53         <td><?php echo $vo['id']; ?></td>
54         <td>产品<?php echo $vo['id']; ?></td>
55         <td>库存<?php echo $vo['id']; ?></td>
56         <td><a href=delete.php?id=<?php echo $vo['id']; ?>  </a>删除</td>
57       </tr>
58     <?php } ?>
59     </tbody>
60   </table>
61   <div id="demo"></div>
62 </div>
63 <script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script>
64 <script>
65   layui.use(['laypage'], function(){
66     var laypage = layui.laypage;
67     laypage.render({
68       elem: 'demo',
69       count: <?php echo $total; ?>,
70       limit: <?php echo $pageSize; ?>,
71       curr: <?php echo $pageNum; ?>,
72       jump: function(obj,first){
73         if(!first){
74           window.location.href = '?page='+obj.curr;
75         }
76       }
77     });
78   });
79 </script>
80 </body>
81 </html>

 

posted @ 2023-09-02 21:19  瑆箫  阅读(62)  评论(0编辑  收藏  举报