vendredi 8 mai 2015

Some misunderstanding with pagination

I trying to make pagination from one post to next post in the single blog post. But seems I don't understand it very well. The problem is that when I open article with ID-1 and I click on next button I get blank/empty page.

This is the post page which get the ID of the chosen post in blog.php. This is what I have so far. Any suggestions?

<?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>";
             }
}
?>                       

Aucun commentaire:

Enregistrer un commentaire