首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对从数据库中获取的数据分页

如何对从数据库中获取的数据分页
EN

Stack Overflow用户
提问于 2016-07-31 07:33:59
回答 2查看 25关注 0票数 0

假设在第一页中有13条记录,第二页是4-8条,第三页是1-3条。

我试过了,但只限于第一页

代码语言:javascript
复制
<?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");
}
?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-31 08:27:51

您的代码应该是这样的

代码语言:javascript
复制
<?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");
}
?>

您的url应该类似于no=1,将"http://localhost/projects/“替换为实际的url。

希望这会有帮助!!

票数 0
EN

Stack Overflow用户

发布于 2016-07-31 07:37:01

下面是一篇讨论这个主题的文章。您需要根据当前页面来偏移查询。所以它将是LIMIT 5 OFFSET <amount based on the page>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38681799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档