donglu1971 2016-07-31 07:33
浏览 16
已采纳

如何对从数据库中提取的数据进行分页

lets say if there are 13 records the latest 5 or from 9-13 in the first page, from 4-8 in second page and 1-3 in the third page

i've tried this but its for the first page only

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mysqli_login";

// Create connection
$connection= new mysqli($servername, $username, $password, $dbname);
$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT 5")or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($query)) {
    $fileName = $row['name'];
    $fileContents = file_get_contents("txt/$fileName");
    $poster = $row['submittedby'];
    $date = $row['trn_date'];
    echo ("posted by :$poster  |  posted date : $date");
    echo ("$fileContents");
}
?>
  • 写回答

2条回答 默认 最新

  • dongtan7998 2016-07-31 08:27
    关注

    Your code should be like this

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "test_db";
    
    // Create connection
    $connection= new mysqli($servername, $username, $password, $dbname);
    
    $current_page = 1; // 1=>refer to the first page, 2=> second page and so on..
    if(!empty($_GET['page_no']){
    $current_page = $_GET['page_no'];
    }
    
    $to = "5"; // it is no of record which you wants to show on each page, you can change it's value as per your need.
    $from = ($current_page - 1) * $to;
    
    
    $query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT $from , $to")or die(mysqli_error($connection));
    while ($row = mysqli_fetch_array($query)) {
        $fileName = $row['name'];
        $fileContents = file_get_contents("txt/$fileName");
        $poster = $row['submittedby'];
        $date = $row['trn_date'];
        echo ("posted by :$poster  |  posted date : $date");
        echo ("$fileContents");
    }
    ?>
    

    your url should be something like http://localhost/projects/?page_no=1 replace "http://localhost/projects/" with your actual url

    Hope this will help!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器