对分页的一些误解

Some misunderstanding with pagination

我正在尝试在单个博客 post 中从一个 post 分页到下一个 post。问题是,当我打开 ID-1 的文章并单击 'next' 按钮时,我得到一个 blank/empty 页面。

这是post页面,获取blog.php中选择的post的ID。这是我到目前为止所拥有的。有什么建议吗?

<?php
     // include database connection
     require_once 'database.inc.php';
     $pdo = Database::connect();
       if(isset($_GET['post_id']) && is_numeric($_GET['post_id'])){
                $post_id = $_GET['post_id'];
     // page is the current page, if there's nothing set, default is page 1
     $page = isset($_GET['page']) ? $_GET['page'] : 1;

     // set records or rows of data per page
     $recordsPerPage = 1;

     // calculate for the query LIMIT clause
     $fromRecordNum = ($recordsPerPage * $page) - $recordsPerPage;

     // select all data
     $query = "SELECT * FROM posts WHERE post_id = $post_id LIMIT {$fromRecordNum}, {$recordsPerPage}";

     $stmt = $pdo->prepare( $query );
     $stmt->execute();

     //this is how to get number of rows returned
     $num = $stmt->rowCount();

     //check if more than 0 record found
     if($num>0){                           
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){               

                     echo ' 
                           // post body ';     
                }
    }                                                 

         // *************** <PAGING_SECTION> ***************

             // ***** for 'first' and 'previous' pages
             if($page>1){       
                 // ********** show the previous page
                 $prev_page = $page - 1;
                 echo "
                <a href='" . $_SERVER['PHP_SELF'] . "?page={$prev_page}'>
                    <div class='prev-btn control-nav text-left'>
                        <h5>Previous Post</h5>
                    </div>
                </a>";     
             }
             // find out total pages
             $query = "SELECT COUNT(*) as total_rows FROM posts";
             $stmt = $pdo->prepare( $query );
             $stmt->execute();

             $row = $stmt->fetch(PDO::FETCH_ASSOC);
             $total_rows = $row['total_rows'];

             $total_pages = ceil($total_rows / $recordsPerPage);

             if($page<$total_pages){
                 // ********** show the next page
                 $next_page = $page + 1;
                 echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$next_page}'>
                    <div class='next-btn control-nav text-right'>
                        <h5>Next Post</h5>
                    </div>
                </a>";
             }
}
?>                       

你能在一个工作网站上展示它吗?那时会更容易判断出什么问题,但是从代码看来您在锚点 href 部分中犯了 url 错误。

$prev_page = $post_id - 1;
<a href='" . $_SERVER['PHP_SELF'] . "?post_id={$prev_page}'>
               <div class='prev-btn control-nav text-left'>
                   <h5>Previous Post</h5>
               </div>
           </a>"

$next_page = $post_id + 1;

and the link is there made in same manner as previous one

因为我不确定你的服务器 php 自己返回了什么,你不应该添加第二个?其中的符号,如果它已经有了,你应该使用 & 我认为获取参数为?仅在脚本文件名后使用一次。或者您缺少 url.

中的 ID

此外 blank/empty 页面可能会导致该页面上的脚本崩溃,如果您关闭了错误,您将不会在网页中看到它,并且必须转到 php 错误日志文件才能查看为他们

编辑:块引用没有正确显示。

从你的问题来看不是很清楚,但我认为你的问题是这样的,你的代码需要设置post_id参数:

if(isset($_GET['post_id']) && is_numeric($_GET['post_id'])){
    $post_id = $_GET['post_id'];
}

但是在您的下一页和上一页链接中,您没有设置 post_id 参数:

echo "<a href='" . $_SERVER['PHP_SELF'] . "?page={$next_page}'>
    <div class='next-btn control-nav text-right'>
        <h5>Next Post</h5>
    </div>
</a>";

您的代码本质上依赖于显示的单个 post,由 post_id 确定。这是我的建议,更改:

 // select all data
 $query = "SELECT * FROM posts WHERE post_id = $post_id LIMIT {$fromRecordNum}, {$recordsPerPage}";

至:

 // select all data
 $query = "SELECT * FROM posts LIMIT {$fromRecordNum}, {$recordsPerPage}";

当然,那么,您将无法显示特定的 post,但又很难知道您实际尝试的是什么在这里实现。

这是因为您首先获得 $post_id,但随后在链接中放置了 $page,其中不包含您的 ID。尝试像这样更改您的下一个和上一个链接:

<a href='" . $_SERVER['PHP_SELF'] . "?post_id=".$prev_page."'>
<a href='" . $_SERVER['PHP_SELF'] . "?post_id=".$next_page."'>

另外不要忘记在此处更改 $page 的行:

$page = isset($_GET['page']) ? $_GET['page'] : 1;

至此

$page = isset($_GET['post_id']) ? $_GET['post_id'] : 1;

这应该可以解决问题。