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 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件