【问题标题】:How to Make PHP Pagination Start on Page 1 Rather Than Page 0?如何使 PHP 分页从第 1 页而不是第 0 页开始?
【发布时间】:2021-01-17 05:40:30
【问题描述】:

我一直在使用我在网上找到的基本 PHP/PDO 分页脚本。

我已经根据我的需要定制了它(我只需要做一些基本的布局工作);剩下的唯一事情就是让分页以与所有其他专业网站相同的方式工作。我的意思是让分页从第 1 页而不是第 0 页开始。你听说过“第 0 页”吗?我肯定没有! (编辑:我在我的网站上使用此脚本的修改版本并使用 .htaccess 文件将第 0 页的 URL index.php?start=0 更改为 /page/0/。经过反思,我现在可以欣赏让第一页的 URL 包含“start=0”的逻辑,因为 Google 做同样的事情。然而,这个问题仍然存在,因为与 Google 不同,这个脚本表明有一个页面 0。)

以下是相关文件:

配置.php

<?php
   // define database related variables
   $database = 'db';
   $host = 'hostname';
   $user = 'username';
   $pass = 'password';

   // try to connect to database
   $db = new PDO("mysql:dbname={$database};host={$host};port={3306}", $user, $pass);

   if(!$db){

      echo "Error in database connection";
   }
?>

数据.php

<?php 
    //include configuration file
    require 'configuration.php';

    $start = 0;  $per_page = 4;
    $page_counter = 0;
    $next = $page_counter + 1;
    $previous = $page_counter - 1;
    
    if(isset($_GET['start'])){
     $start = $_GET['start'];
     $page_counter =  $_GET['start'];
     $start = $start *  $per_page;
     $next = $page_counter + 1;
     $previous = $page_counter - 1;
    }
    // query to get messages from messages table
    $q = "SELECT * FROM students LIMIT $start, $per_page";
    $query = $db->prepare($q);
    $query->execute();

    if($query->rowCount() > 0){
        $result = $query->fetchAll(PDO::FETCH_ASSOC);
    }
    // count total number of rows in students table
    $count_query = "SELECT * FROM students";
    $query = $db->prepare($count_query);
    $query->execute();
    $count = $query->rowCount();
    // calculate the pagination number by dividing total number of rows with per page.
    $paginations = ceil($count / $per_page);
?>

index.php

<html>
    <head>
        <title>Pagination</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    </head>
    <body>
        <?php include_once 'data.php'; ?>
        <div class="table-responsive" style="width: 600px; border: 1px solid black; margin: 10px;">
            <table class="table table-striped" class="table table-hover">
                <thead class="table-info">
                 <th scope="col" class="bg-primary" >Id</th>
                 <th scope="col" class="bg-primary">First Name</th>
                 <th scope="col" class="bg-primary">Last Name</th>
                 <th scope="col" class="bg-primary">Email</th>
                </thead>
                <tbody>
                <?php 
                    foreach($result as $data) { 
                        echo '<tr>';
                        echo '<td>'.$data['id'].'</td>';
                        echo '<td>'.$data['first_name'].'</td>';
                        echo '<td>'.$data['last_name'].'</td>';
                        echo '<td>'.$data['email'].'</td>';
                        echo '</tr>';
                    }
                 ?>
                </tbody>
            </table>
            <center>
            <ul class="pagination">
            <?php
                if($page_counter == 0){
                    echo "<li><a href=?start='0' class='active'>0</a></li>";
                    for($j=1; $j < $paginations; $j++) { 
                      echo "<li><a href=?start=$j>".$j."</a></li>";
                   }
                }else{
                    echo "<li><a href=?start=$previous>Previous</a></li>"; 
                    for($j=0; $j < $paginations; $j++) {
                     if($j == $page_counter) {
                        echo "<li><a href=?start=$j class='active'>".$j."</a></li>";
                     }else{
                        echo "<li><a href=?start=$j>".$j."</a></li>";
                     } 
                  }if($j != $page_counter+1)
                    echo "<li><a href=?start=$next>Next</a></li>"; 
                } 
            ?>
            </ul>
            </center>    
        </div>  
    </body>
</html>

我认为这可能只是将一些 0 更改为 1 的问题。我已经使用这种方法尝试了一些组合,但我不确定我在做什么。

文章提到如何改变这种分页特性的最接近的说法是,“在第一页上,页面计数器为 0,存储在 $page_counter 中。”因此,像我这样的人可能会得出结论,您只需将该变量的值更改为 1。如果就这么简单就好了!

【问题讨论】:

  • 给自己找另一篇文章吧。这一点毫无意义。
  • 变量太多,对最终结果毫无用处。
  • 您尝试过什么让它发挥作用?为什么不先用echo "&lt;li&gt;&lt;a href=?start='0' class='active'&gt;0&lt;/a&gt;&lt;/li&gt;" 调整行?

标签: php pdo pagination


【解决方案1】:

这是我将如何修改它(我没有办法对此进行测试,因此可能存在一些错误): PHP:

<?php 
    //include configuration file
    require 'configuration.php';

    $start = 0;
    $per_page = 4;
    $page_counter = 1;
    $next = $page_counter + 1;
    $previous = $page_counter - 1;
    
    if(isset($_GET['start'])){
     // $start = $_GET['start'];
     $page_counter = (int) $_GET['start'];
     $start = ($page_counter - 1) *  $per_page;
     $next = $page_counter + 1;
     $previous = $page_counter - 1;
    }
    // query to get messages from messages table
    $q = "SELECT * FROM students LIMIT $start, $per_page";
    $query = $db->prepare($q);
    $query->execute();

    if($query->rowCount() > 0){
        $result = $query->fetchAll(PDO::FETCH_ASSOC);
    }
    // count total number of rows in students table
    // Feels like doing "SELECT COUNT(*) as qty FROM students" may save some memory for the script as in this case you won't be loading entire table into your PDO library but just the total number of records value.
    $count_query = "SELECT * FROM students";
    $query = $db->prepare($count_query);
    $query->execute();
    $count = $query->rowCount();
    // calculate the pagination number by dividing total number of rows with per page.
    $paginations = ceil($count / $per_page);
?>

HTML:

<html>
    <head>
        <title>Pagination</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    </head>
    <body>
        <?php include_once 'data.php'; ?>
        <div class="table-responsive" style="width: 600px; border: 1px solid black; margin: 10px;">
            <table class="table table-striped" class="table table-hover">
                <thead class="table-info">
                 <th scope="col" class="bg-primary" >Id</th>
                 <th scope="col" class="bg-primary">First Name</th>
                 <th scope="col" class="bg-primary">Last Name</th>
                 <th scope="col" class="bg-primary">Email</th>
                </thead>
                <tbody>
                <?php 
                    foreach($result as $data) { 
                        echo '<tr>';
                        echo '<td>'.$data['id'].'</td>';
                        echo '<td>'.$data['first_name'].'</td>';
                        echo '<td>'.$data['last_name'].'</td>';
                        echo '<td>'.$data['email'].'</td>';
                        echo '</tr>';
                    }
                 ?>
                </tbody>
            </table>
            <center>
            <ul class="pagination">
            <?php
                if($page_counter > 1)
                    echo "<li><a href=?start=$previous>Previous</a></li>";
                for($j=1; $j <= $paginations; $j++) {
                  if($j == $page_counter) {
                     echo "<li><a href=?start=$j class='active'>". $j ."</a></li>";
                  } else {
                     echo "<li><a href=?start=$j>". $j ."</a></li>";
                  } 
                }
                if($page_counter < $paginations)
                    echo "<li><a href=?start=$next>Next</a></li>"; 
            ?>
            </ul>
            </center>    
        </div>
    </body>
</html>

【讨论】:

  • 您好,感谢您的回复。我相信您的更正会更改链接,但不会更改 URL。这是我的错,因为没有更具体。基本上,我希望第一页的 URL 类似于“index.php?start=1”而不是“index.php?start=0”。当然,“1”也应该是第一页的链接。
  • 我明白了。您将不得不修改后端逻辑以及视图。基本上只需将所有数字上移 1。然后在您的查询中执行类似 "SELECT * FROM students LIMIT" . $start -1 . ", $per_page" 的操作
  • 再次感谢。听起来你知道该怎么做。我还在学习 Stack Overflow(更不用说 PHP),但你知道你是否可以编辑你的答案吗?如果是这样,您能否进行所有编辑并在适用的地方再次添加// Updated this line cmets?话虽如此,我有点理解让 URL 以“index.php?start=0”开头的逻辑。谷歌做了类似的事情。但我的 URL 看起来像这样:“/page/0/”。所以我想你可以理解为什么我希望 1 成为第一页的 URL。
  • 做了一些修改。更新了好几行,所以根本没有放 // Updated this like 评论。
  • 你离得太近了!只有一个错误:在第一页(index.php 或 index.php?start=1)上,如果有多个页面,它总是显示总页数减 1。例如:如果有 2 页,则只显示第 1 页;如果有 3 页,则只显示第 1 页和第 2 页;如果有 4 页,则仅显示第 1、2 和 3 页。如果有 1 页,则仅显示第 1 页。请注意,这适用于 index.php?start=1 或 index.php?start=1。 php。所有其他页面似乎都完美无瑕。如果你能解决这个问题,我会很高兴的!
猜你喜欢
  • 2017-02-14
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 2012-12-24
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多