【问题标题】:Pagination to display a limit per page分页显示每页的限制
【发布时间】:2018-04-19 22:14:12
【问题描述】:

如何确保我的页面每页最多可以存储 3 个我从数据库中获得的结果?我将通过 html 链接从上一页中检索一些值,并使用它从数据库中检索结果。然后从结果来看,我希望把它们做成页面,这样就不用滚动太久了。并且在每个打开的页面上,它都会被禁用。

<?php
if (isset($_GET["w1"]) && isset($_GET["w2"])) {
        $lt = $_GET["w1"];
        $ln = $_GET["w2"];
        $GLOBALS['id']= "";
    }

    $servername = "localhost";
    $username = "root";
    $password = "";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e){
        echo "Connection failed: " . $e->getMessage();
    }

    function getAddress($lt, $ln) {
        $result = json_decode(file_get_contents("https://maps.google.com/maps/api/geocode/json?key=". API_KEY ."&latlng=$lt,$ln"));
        if ($result->status == 'OK') {
            return $result->results[0]->formatted_address;
        }
        return 'Error'; 
    }

    try{
        $db = $conn->prepare("Select ID from Table");
        $db->execute();

        echo "<div class='row'>";
        while($row=$db->fetch(PDO::FETCH_OBJ)) {
            $GLOBALS['id'] = $row->ID;

            echo "<div class='col-sm-6 col-md-4'>";

            echo  "<h4 class='media-heading'>", $GLOBALS['id'] ,"</h4>";
            echo    "<span class='fa fa-map-pin'></span> ", getAddress($lt,$ln);
            echo "</div>";

        }
        echo "</div>";

    } catch (PDOException $e) {
        echo "Error: ".$e;
    }


?>
<!--Pagination-->
<div class="row">
    <div class="col-xs-12">
        <nav aria-label="Page navigation" class="text-center">
          <ul class="pagination pagination-lrr">
            <li>
              <a href="#" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
              </a>
            </li>
            <li class="active"><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>
            <li>
              <a href="#" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
              </a>
            </li>
          </ul>
        </nav>
    </div>
</div>

【问题讨论】:

    标签: php html mysql pagination


    【解决方案1】:

    您必须通过添加 2 个变量来稍微修改您的代码:

    • currentPage - 告诉脚本访问者在哪个页面
    • pagepage - 每页应显示多少条记录

    下一步是对 SQL 查询使用 LIMIT 选项。

    LIMIT start, amount
    

    我们将使用 currentPage 和 perpage 变量计算起点。

    $currentPage = (!isset($_GET['current_page']) || (int)$_GET['current_page'] == 0) ? 1 : $_GET['current_page'];
    $perpage = 3; //or whatever you'd like
    
    $limit = $perpage;
    $start = ($currentPage-1)*$perpage;
    
    $db = $conn->prepare("Select ID from Table LIMIT $start, $limit");
    

    所以对于第 1 页:

    start = (1-1)*3 = 0
    limit = 3
    Which means: get the first 3 items.
    

    对于第 2 页:

    start = (2-1)*3 = 3
    limit = 3
    Which means: count 3 items and then get the 3 after to them
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-31
      • 2012-06-25
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      相关资源
      最近更新 更多