技术网文章:PHP学习_PHP+MySQL+LayUI分页查询显示
html
构修前端样式,AJAX
同步哀求数据,再使用layui.table
数据表格的要领衬着,如许便实现了分页查询隐示。
html
构修前端样式
AJAX
同步哀求数据
使用layui.table
数据表格的要领衬着。
1.HTML文件
<p class="layui-card-body "> <table id="demo" class="layui-hide"></table> <p id="pageUD"></p></p><script src="js/jquery.min.js"></script><script> var pageNum = 0; var limit = 10; var page = 1; $.ajax({ url: "laypage.php", async: false, type: "post", success: function (res) { pageNum = res; //与到数据总条数 // console.log(res) } }); layui.use('table', function () { var table = layui.table; table.render({ elem: '#demo', method: 'post', url: 'paging.php', limit: limit, page: page, cellMinWidth: 80, //齐局界说通例单位格的最小严度,layui 2.2.1 新删 cols: [[ {checkbox: true}, {field: 'id', width: 80, sort: true, title: 'ID'}, {field: 'donor', width: 240, sort: true, title: '姓名/昵称'}, {field: 'object', width: 180, sort: true, title: '捐助名目'}, {field: 'money', width: 150, sort: true, title: '捐助金额'}, {field: 'time', width: 200, sort: true, title: '捐助实战'}, {field: 'type', width: 100, sort: true, title: '捐助类型'}, {field: 'message', width: 200, title: '备注/留言'} ]] }); });</script>
畴前端获与page以及limit二个变质,交给MySQL外的 limit
举行分页查询,将查询的成果拼拆后之前端LayUI框架划定的json情势返归。
2.laypage.php 文件
laypage.php 功效是获与数据总数并返归给前端铺示。
<?php require ('db_config.php'); $sql = 'select count(*) from donation_copy1'; $result = $mysqli->query($sql); $sum = mysqli_fetch_row($result); echo ceil($sum[0]/1); ?>
3.paging.php 文件
laypage.php 功效是按照前端通报的变质指定参数分页查询数据并返归给前端铺示。
<?php header("content-type:text/html;charset=utf-8;"); require ('db_config.php');$limit = $_POST['limit']; $page = $_POST['page'];$new_page = ($page - 1)*$limit; $sql = "select * from donation_copy1 order by id desc limit " .$new_page.",".$limit; $result = $mysqli->query($sql); $sql2 = 'select * from donation_copy1'; $count = mysqli_num_rows($mysqli->query($sql2)); $arr = array(); while ($row = mysqli_fetch_array($result)) { $arr[] = $row;}$donation_data = array( // 拼拆成为前端需求的JSON 'code'=>0, 'msg'=>'', 'count'=>$count, 'data'=>$arr); echo json_encode($donation_data); //echo $sql; ?>
保举:《2021年PHP口试题年夜汇总(保藏)》《php望频学程》
以上便是PHP+MySQL+LayUI分页查询隐示的具体内容,更多请存眷php外文网其它相干文章!
【酷吧易】
有用么