-1

I am trying to make pagination from one post to the next post in a single blog post. The problem is that when I open an article with ID-1 and I click on the 'next' button I get a blank/empty page.

This is the post page, which gets 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>";
             }
}
?>                       
Mark
  • 2,380
  • 11
  • 29
  • 49
Select
  • 65
  • 2
  • 13

3 Answers3

1

Could you show it in a working website? It would be easier to tell what is wrong then, but from the code it looks like you are making the url wrong in your anchor href part.

$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

as i am not sure what your server php self is returning and you shouldnt be adding the second ? symbol in it, if it already had it and u should use & i think for get parameters as ? is only used once after the script file name once. Or you are either missing the ID in your url.

Also blank/empty page might be crashing the script on that page and if u have errors turned off you wont see it in webpage and will have to go to your php errors log file to look for them

EDIT: as block quote wasnt showing correctly.

Community
  • 1
  • 1
Marius
  • 11
  • 4
  • I'm sorry but is on my local yet. So I've tried to change `?` to `&` but on next page I got `Not found...` – Select May 08 '15 at 13:56
  • ok as i see from your comment in Harrys response, you need new blog post shown, so if you are sure that your all blogs are going in consecutive order you only need to add or take away 1 from your post id variable. – Marius May 12 '15 at 11:36
1

It's not very clear from your question, but I think your problem is this, your code requires the post_id parameter to be set:

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

But in your next and prev page links, you're not setting a post_id parameter:

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

Your code is inherently relying on a single post being shown, as determined by the post_id. Here's what I suggest, change:

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

to:

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

of course, then, you won't be able to show a specific post, but then again it's hard to know what you're actually trying to achieve here.

Harry Mustoe-Playfair
  • 1,369
  • 16
  • 29
  • Tried this but again nothing shown in next page. Just tried when from `blog.php` I choose post whit `post_id=1` to load in `post.php` which is ok so far. Then under the post I have 2 buttons for next post i.e. post_id=2 ot previous post – Select May 08 '15 at 14:01
  • Right, so you want the next page button to actually show the next *post*? In which case, you'd need something like the following for getting the `post_id` for the next and previous pages: http://stackoverflow.com/questions/1446821/how-to-get-next-previous-record-in-mysql – Harry Mustoe-Playfair May 08 '15 at 14:11
  • Yes, thats it what I need `so you want the next page button to actually show the next post` – Select May 08 '15 at 14:17
0

It is because first you get $post_id but then in the links you put $page which doesn't contain your ID. Try to change your nex&prev links like this:

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

Also don't forget to change the line for $page here:

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

to this

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

This should do the trick.

John
  • 171
  • 1
  • 2
  • 12
  • Thank's for your answer. Yes, I knew it that the problem is in the link because I didn't give next ID of the post but it's given next page which is empty w/o post_id. And yes your answer work perfectly. Thank you again! – Select May 11 '15 at 05:24