Pagination for Published post

This forum is great,helping me lot… after learning Core php, im trying to develop very simple CMS.
While developing blog post, im trying have pagination which works fine but issue is that im getting pagination for draft post too. i want pagination for published post only ($post_status == ‘published’), so how can i get pagination for post with post status published…
or is there any better way for pagination then my code…

<?php 
    if(isset($_GET['page'])){
        $page = $_GET['page'];
    }else{
        $page = "";
    }

    if($page == "" || $page == 1){
        $page_count = 0;
    }else{
        $page_count = ($page * 5) - 5;
    }

    // query for pagination
    $query_count = "SELECT * FROM posts";
    $find_count = mysqli_query($connection,$query_count);
    $count = mysqli_num_rows($find_count);
    $count = ceil($count/5);

    // Query for post
    $query = "SELECT * FROM posts LIMIT  $page_count,5";
    $select_post = mysqli_query($connection,$query);
        while($row = mysqli_fetch_assoc($select_post)){
            $post_id= $row['post_id'];
            $post_title = $row['post_title'];
            $post_author = $row['post_author'];
            $post_tags = $row['post_tags'];
            $post_date = $row['post_date'];
            $post_image = $row['post_image'];
            $post_content = substr($row['post_content'],0,100);
            $post_status = $row['post_status'];

            if($post_status == 'published'){
?>


<!-- Blog Post -->
<h2>
    <a href="post.php?p_id=<?php echo $post_id; ?>"><?php echo $post_title ?></a>
</h2>

<p class="post">
    by <a href="author_post.php?author_id=<?php echo $post_author; ?>"><?php echo $post_author ?></a><br>
    by <a href="index.php"><?php echo $post_tags ?></a>
</p>

<p><span class="glyphicon glyphicon-time"></span> <?php echo $post_date ?></p>

<img class="img-responsive" width="300" src="images/<?php echo $post_image ;?>" alt="">

<p><?php echo $post_content ?></p>

<?php        
     }}
 ?>
<!-- Blog Post -->

<!-- pagination -->
<ul class="pager">
<?php 
    for($i = 1; $i <= $count; $i++){
        echo"<li><a href='index.php?page={$i}'>{$i}</a></li>";
    }
 ?>
</ul>
<!-- pagination -->

Solved… it was really simple…

// query for pagination
$query_count = "SELECT * FROM posts WHERE post_status = 'published'";
$find_count = mysqli_query($connection,$query_count);
$count = mysqli_num_rows($find_count);
$count = ceil($count/5);

Glad you solved your problem.

If you’re starting out, it would be a good idea to use PDO rather than the mysqli interface for your database interaction.

You can find a good tutorial in PDO here.

Yes. The hard-coded 5 should be changed to use a variable so that you can easily change the number of rows per page. Also, the first query, to get the total number of matching rows, should instead use SELECT COUNT(*) as total …, then you fetch the total from that query. The current query is selecting all the column and all the matching rows of data, just to get a count.

i will learn it… thank for recommendation…

im new to the code you present hope you mean as below:

$query_count = "SELECT COUNT(*) as total FROM posts WHERE post_status = 'published'";
                $find_count = mysqli_query($connection,$query_count);
                $dop = mysqli_fetch_assoc($find_count);
                $count = $dop['total'];
                $count = ceil($count/5);

Pagination Work Fine in index.php but failed in Admin Dashboard
Cms Folder structure:


Post.php which is inside admin folder with following code im pulling files from includes folder

if(isset($_GET['source'])){
    $source = $_GET['source'];
}else{
    $source = '';
}

switch ($source){

    case 'add_post';
    include'includes/add_post.php';
    break;

    case 'edit_post';
    include'includes/edit_post.php';
    break;

    default:

    include "includes/view_all_post.php";

    break;

}

All the post from database is being displayed in View_all_post.php
I have done the same process as above but pagination didnt work for admin section
This is not working in view_all_post.php but if i put same code in post.php it works fine but pagination displays in other page too… how can i solve it…

<?php 
     if(isset($_GET['page'])){
            $page = $_GET['page'];
        }else{
            $page = "";
        }

        if($page == "" || $page == 1){
            $page_1 = 0;
        }else{
            $page_1 = ($page * 5) - 5;
        }

    $post_count = "SELECT * FROM posts";
    $find_count = mysqli_query($connection,$post_count);
    $count = mysqli_num_rows($find_count);
 ?>

//here is the code for display post in admin-section

<ul class="pager">
    <?php 
         for($i = 1; $i <= $count; $i++){
             echo"<li><a href='includes/view_all_post.php?page={$i}'>{$i}</a></li>";
         }
    ?>
</ul>

The links you output on the page should NOT be to the files in the ‘includes’ folder. That folder is just where the .php files are stored at on the server. Your code is (should be) requiring one of those files into a ‘main’ page file based on the $_GET['source'] value. You should be using relative links, so that when someone clicks on one, the browser starts with the url of the current ‘main’ page and appends the relative link information to it to produce the full url that gets requested.

Next, the pagination links should propagate any existing get parameters, so that things like $_GET['source'] and search/filter values will be carried from page to page. The simple way of doing this is to get a copy of the existing $_GET array (so that the original is unaltered and can be used elsewhere in your code), set the ‘page’ element in the copy to the value you want for each link, use http_build_query() to produce the query-string part of the url, then output each link.

Lastly, see where you are writing out line after line of code in the switch/case statement for every possible value. This is not general purpose code. It has you finding and editing program logic every time you add or change something and for every different main web page you create. Instead use a ‘mapping’ array, with the input values as the array indexes and the path/filenames as the array values. You would also have a ‘default’ index entry with the default path/filename. The logic would then test if the input value isset() in the array to get the correct path/name to ‘require’ (your code should use require for things is must have in order to work) or use the default entry if the input value index isn’t set in the array.

As i have just start coding, i only got few things from what you said but really appricate for your effort and time for helping beginner like me

echo"<li><a href='post.php?page={$i}'>{$i}</a></li>";
previously i used above line but it was not working but now its working fine may some issue with local server…i understand how its working.

Your line make me more clear…

The links you output on the page should NOT be to the files in the ‘includes’ folder. That folder is just where the .php files are stored at on the server.

Are you telling me to used required instead of includes… ?? and dont use switch statement for my view page purpose…???

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.