Problem with pagination

Hello,I have a problem with pagination,what’s happening,first category posts works great but, others categories when i click on second page bring me back again to the first category second page…anyone who can help with this
I appreciate that…tnx

this is my code

 <?php 

                            if(isset($_GET['category'])) {

                            $post_category_id = $_GET['category'];   


                            }

                            $per_page = 30;


                            if(isset($_GET['page'])) {


                            $page = $_GET['page'];

                            } else {


                            $page = "";
                            }


                            if($page == "" || $page == 1) {

                            $page_1 = 0;

                            } else {

                            $page_1 = ($page * $per_page) - $per_page;

                            }

                            $find_count = mysqli_query($connection,$post_query_count);
                            $count = mysqli_num_rows($find_count);

                            if($count < 1) {


                            echo "<h1 class='text-center'>No posts available</h1>";


                            } else {


                            $count  = ceil($count /$per_page);

 $query = "SELECT * FROM posts WHERE post_category_id = $post_category_id ORDER BY post_id DESC LIMIT $page_1, $per_page";
                            $select_all_posts_query = mysqli_query($connection, $query);

                            while($row = mysqli_fetch_assoc($select_all_posts_query)) {
                            $post_id = $row['post_id'];
                            $post_title = $row['post_title'];
                            $post_title_head = $row['post_title_head'];
                            $post_image = $row['post_image'];
                            $post_status = $row['post_status'];
                            ?>
                            <div class="col-sm-6 col-md-3 isotope-item web-design">
                            <div class="image-box">
                            <div class="overlay-container">
                            <a href="post.php?p_id=<?php echo $post_id; ?>">
                            <img class="img-rensposive" src="images/<?php echo $post_image; ?>" alt="Image">                          
                            </a>
                            <a href="post.php?p_id=<?php echo $post_id; ?>" class="overlay small">
                            <i class="fa fa-link"></i>
                            <span><?php echo $post_title_head ?></span>
                            </a>
                            </div>
                            <a href="post.php?p_id=<?php echo $post_id; ?>" class="btn btn-light-gray btn-block">
                            <?php echo $post_title ?>
                            </a>
                           </div>
                        </div>
                   <?php   } }?>
          </div>
</div>

                            <ul class="pagination">

                            <?php 

                            $number_list = array();


                            for($i =1; $i <= $count; $i++) {


                            if($i == $page) {

                            echo "<li '><a class='active_link' href='category.php?page={$i}'>{$i}</a></li>";


                            }  else {

                            echo "<li '><a href='category.php?page={$i}'>{$i}</a></li>";

                            }

                      }

            ?>

            </ul>
        </div>
</section>

you do not set any category in the pagination links.

1 Like

Thank you for help chorn i fix that now work fine,but do u have idea why i got 13 pages where is only 4 pages with articles others pages are empty…

below is fixed pagination link if someone need…

<?php 
    $number_list = array();
    for($i = 1; $i <= $count; $i++) {
        if($i == $page) {
            echo "<li><a href='category.php.php?page=".$i."&category=".$cat_id."'>".$i."</a></li> ";
        }  else {
            echo "<li><a href='category.php?page=".$i."&category=".$cat_id."'>".$i."</a></li> ";
        }
    }
?>

just go back from where your defined $count and have a look at all involved variables via var_dump, it looks like you got more results from your database as expected.

Yes, your code doesn’t show the query string where you figure out the post count. I can’t see where you put the category into that query, for example.

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