【问题标题】:Pagination with PHP and MYSQL using Boostrap使用 Bootstrap 使用 PHP 和 MYSQL 进行分页
【发布时间】:2017-03-21 07:25:45
【问题描述】:

我的数据库中有很多数据(包括图片、描述等),哪些数据不能合理地留在一页中。 如何使用 php、mysql 和 bootstrap 进行分页?

 <?php

    $query = "SELECT Title, Painter_Name, Description, Year, Image FROM paintings, painters WHERE painters.ID = paintings.Painter_ID";

        $query_run = mysqli_query($connection, $query);

        while($row = mysqli_fetch_assoc($query_run)) {
      $title = $row['Title'];
      $painter = $row['Painter_Name'];
      $description = $row['Description'];
      $image = $row['Image'];
      $year = $row['Year'];
    ?>

      <div id="contatiner">

    <?php
      echo "
      <div class=\"row\">
        <div class=\"offset-sm-2 col-sm-8 offset-sm-2\"> 
            <div class=\"card\">
              <div class=\"card-block\">
                <h4 class=\"card-title text-sm-center\">$title ($year)</h4>
                <h6 class=\"card-subtitle text-sm-center text-muted\">$painter</h6>
              </div>
              <p class=\"text-sm-center\"><img style=\"width:300px; height:300px;\" src=\"$image\" alt=\"Card image\"></p>
              <div class=\"card-block\">
                <p class=\"card-text text-sm-center\">$description</p>
                <p class=\"text-sm-center\"><a href=\"#\" class=\"card-link\">Add to favorites</a></p>
              </div>
            </div>
        </div>
      </div>
         ";
        }
    ?>
    </div>

<nav class="text-xs-center">
    <ul class="pagination">
        <li class="page-item">
            <a href="#" class="page-link" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
                <span class="sr-only">Previous</span>
            </a>
        </li>
        <li class="page-item"><a class="page-link" href="#">1</a></li>
        <li class="page-item"><a class="page-link" href="#">2</a></li>
        <li class="page-item"><a class="page-link" href="#">3</a></li>
        <li class="page-item"><a class="page-link" href="#">4</a></li>
        <li class="page-item"><a class="page-link" href="#">5</a></li>
        <li class="page-item">
            <a href="#" class="page-link" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
                <span class="sr-only">Next</span>
            </a>
        </li>
    </ul>
</nav>

我已经浏览了互联网,但现在还不知道从什么开始......

提前致谢!

【问题讨论】:

    标签: php mysql twitter-bootstrap pagination


    【解决方案1】:

    您可以使用限制和偏移量进行查询:https://dev.mysql.com/doc/refman/5.5/en/select.html

    有两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。初始行的偏移量为0(不是1):

    SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

    例如,第 1 页的偏移量为 0,限制为 10,并且每页您将偏移量和限制增加 10(或任何您想要的)。

    您可以使用 $_GET 变量指定页面 id,或使用不同的样式; MVC 或获取部分 url。

    【讨论】:

    • 请尽可能链接到官方文档。 w3schools 是出了名的问题。
    • 我检查了页面,这是一个不错的示例,但确定.. 已修复。
    • 官方文档的 cmets 部分通常有非常有用的示例。仅此一项就让他们变得更好。
    猜你喜欢
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 2015-05-16
    相关资源
    最近更新 更多