溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP+MySQL+LayUI分頁查詢顯示的方法

發(fā)布時間:2021-04-13 09:18:41 來源:億速云 閱讀:542 作者:小新 欄目:編程語言

這篇文章主要介紹PHP+MySQL+LayUI分頁查詢顯示的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

  • html構(gòu)建前端樣式

  • AJAX異步請求數(shù)據(jù)

  • 使用layui.table數(shù)據(jù)表格的方法渲染。

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; //取到數(shù)據(jù)總條數(shù)
            // 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, //全局定義常規(guī)單元格的最小寬度,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 進行分頁查詢,將查詢的結(jié)果拼裝后以前端LayUI框架規(guī)定的json形式返回。

2.laypage.php 文件

laypage.php 功能是獲取數(shù)據(jù)總數(shù)并返回給前端展示。

<?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 功能是根據(jù)前端傳遞的變量指定參數(shù)分頁查詢數(shù)據(jù)并返回給前端展示。

<?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;
    ?>

最終頁面效果如下所示:
PHP+MySQL+LayUI分頁查詢顯示的方法

以上是“PHP+MySQL+LayUI分頁查詢顯示的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI