duanpei4455 2016-06-18 22:51
浏览 108
已采纳

限制每页显示的数据库条目数

How would we select just, lets say 12 database entries per page, and then push 12 more onto the next page and so on and so forth?

This would be implementing pagination from boostrap, so:

<ul class="pagination">
  <li><a href="#">1</a></li>
  <li><a href="#">2</a></li>
  <li><a href="#">3</a></li>
  <li><a href="#">4</a></li>
  <li><a href="#">5</a></li>
</ul>

and if it helps here is some php, this is just the basic form that I used to grab and display the entires, but they all appear on one page right now.

$id = (empty($_GET['id'])) ? : $_GET['id'] ;

$query = "SELECT title,whole FROM stories ";
$stmt = $db->prepare($query);

$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//delete this-> $info = mysql_fetch_array($query1);   ?
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{

    ?>
    <div class="left">
       <a href="">
          <span class="text-content"><span><?php echo htmlentities($row["whole"]); ?></span></span>
       </a>
    </div>
    <?php
}

?>
  • 写回答

2条回答 默认 最新

  • droos02800 2016-06-18 23:04
    关注

    Based on your code examples, you could easily use LIMIT and OFFSET in your query to enable pagination.

    SELECT title, whole FROM stories LIMIT 12 OFFSET 0
    

    The above query would show the 12 results.

    <?php
    $per_page = 12;
    $id = (empty($_GET['id'])) ? 0 : $_GET['id'] ;
    $offset = $per_page * $id;
    $query = "SELECT title, whole FROM stories LIMIT :limit OFFSET :offset";
    $stmt = $db->prepare($query);
    $stmt->bindParam(":limit", $per_page, PDO::PARAM_INT);
    $stmt->bindParam(":offset", $offset, PDO::PARAM_INT);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    //delete this-> $info = mysql_fetch_array($query1);   ?
    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
    
        ?>
        <div class="left">
           <a href="">
              <span class="text-content"><span><?php echo htmlentities($row["whole"]); ?></span></span>
           </a>
        </div>
        <?php
    }
    

    This isn't tested, but it should point you in the right direction.

    Here is a link to a more in-depth explanation.

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

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀