如何使用querylimit、offset和jquerydatatable分页?

von4xj4u  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(408)

我用数据表做了一页。但是,随着数据数量的增加,数据加载的速度会减慢,因此我们尝试继续分页。但我不知道如何在当前代码中修改它。该如何修改?
在我的数据表scipt中

$(function() {

          $('#datatable1').DataTable({
            responsive: true,
            order: [[ 1, "desc" ], [ 2, "desc"]],
            language: {

              searchPlaceholder: 'Search...',
              sSearch: '',
              lengthMenu: '_MENU_ items/page',
            },
            dom : 'Bfrtip',

            buttons: [
                'copy', 'excel', 'print'
            ]
          });
        });

在此sql查询中

$sql = "SELECT id,  DATE_FORMAT(tranDate,'%Y-%m-%d') as tranDate, tranTime, date, cmd, paymethod, amount, orgRefNo, orgTranDate FROM noti_card_data  WHERE  mbrNO = '1'";

in this table view code

    if($result = mysqli_query($link, $sql)){               

            if(mysqli_num_rows($result) > 0){

                echo "<table id='datatable1' class = table style = 'width: 100%; font-size:12.5px; font-family:nanum; background-color:transparent;'>";
                echo "<thead >";
                echo "<tr>";
                echo "<th>No</th>";
                echo "<th>amount</th>";
                echo "</tr>";
                echo "</thead>";

                echo "<tbody>";
            while($row = mysqli_fetch_array($result)){
                echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . number_format($row['amount']) . "</td>";
                echo "</tr>";
            }
                echo "</tbody>";                            
                echo "</table>";
                // Free result set
                mysqli_free_result($result);                                  
            } 

            else{

            }
        }     

    else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    } 

    // Close connection
    mysqli_close($link);

在这个视图中

如何使用分页

hlswsv35

hlswsv351#

通过获取分页所在的当前页所需的数据集,而不是获取查询返回的所有数据集,可以实现这一点。
例如:
假设您在第1页的分页中,如果您每页只需要100个项目,那么您可以将查询检索的项目限制为100个,这样做可以更快地检索数据,因为您只需要在当前页中显示100个项目,而不是从查询中获取每一个数据集。
您必须在url中传递页码。想象一下你的网址就像下面提到的那样。
http://localhost/pagination.php?page=2

// getting the current page to set the starting point to get data.
    if (isset($_GET['page'])) {
      $curPage = $_GET['page'];
    } else {
      $curPage = 1;
    }

    //number of items you need per a page
     $labels_per_page = 100;

     //setting the starting point according to the page no.                  
 if($curPage>1){
     $start = ($curPage-1)*$labels_per_page;
  }else{
     $start = 0;
  }                     

//limiting the query according to the starting point nd items per page
$sql = "SELECT id,  DATE_FORMAT(tranDate,'%Y-%m-%d') as tranDate, tranTime, date, cmd, paymethod, amount, orgRefNo, orgTranDate FROM noti_card_data  WHERE  mbrNO = '1' LIMIT ".$start .','.$labels_per_page ;

结果视图

我要看风景

相关问题