繁体   English   中英

如何使 PHP 分页从第 1 页而不是第 0 页开始?

[英]How to Make PHP Pagination Start on Page 1 Rather Than Page 0?

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

我已经根据我的需要定制了它(我只需要做一些基本的布局工作); 剩下的唯一事情就是让分页以与所有其他专业网站相同的方式工作。 我的意思是让分页从第 1 页而不是第 0 页开始。你听说过“第 0 页”吗? 我肯定没有:(编辑,我在我的网站上使用了这个脚本的修改版本,并使用了一个 .htaccess 文件来更改第 0 页的 URL。索引?ZE1BFD762321E409CEE4AC0B6E841903 到 /CZ/03 的反射 = /CZ/03。 ,我现在可以理解让第一页的 URL 包含“start = 0”的逻辑。因为谷歌做了同样的事情,然而,这个问题仍然存在,因为与谷歌不同。这个脚本表明有一个页面 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。如果就这么简单就好了!

这是我将如何修改它(我没有办法对此进行测试,因此可能存在一些错误):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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM