Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pagination in PHP not showing data when page is changed

I am new to PHP and web development. I got a sample code for paginations from web which I further modified as per my requirement.

Issue :- On my website, there are categories in the navigation menu , when they are clicked they redirect to there respective page and show data related to that particular category. On that page I have created a pagination , max data which can be shown on 1 page is 9. Now when the page loads and active pagination is 1 everything works fine, but when I change the page no data gets loaded on other pages(ex. 2,3,4 or hitting next or last).

What I have tried:-I think, somewhere I have understood the issue when I was clicking on different pagination numbers. When the category on home page is clicked the URL takes a "cat_id" with it to the next page that’s why first page is loading fine, but when other pagination pages are clicked URL does not gets the "cat_id". I tired to send the cat_id on other pages also but it did not worked. I am not that much experienced in PHP to try anything further.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here are some image of Home page and next page.Home Page with category menu

$limit = 9;
$current_page = 1;
if(isset($_GET['page'])){
    $page = $_GET['page'];
    if($page<=0){
      $page=1;
      $current_page=1;
    }else{
      $current_page = $page;
    }
    
}else{
    $page = 1;
}
$offset = ($page - 1) * $limit;

// FETCHING IMAGES FROM DATABASE
        
            $cat_id = $_GET['cat_id'];
            $sql = "SELECT * FROM `image_files` where `cat_id` = $cat_id ORDER BY `img_id` DESC LIMIT $offset,$limit ";
            $result = mysqli_query($conn, $sql);

?>
<div class="thumbnail-wrapper">

<?php
    if(mysqli_num_rows($result)>0){  
    while($row = mysqli_fetch_assoc($result)){
?>
    <div class="card">
        <img data-id="download.php?file=<?php echo $row['img_name'] ?>" src="uploaded/<?php echo $row['img_name'] ?>"
            alt="">
    </div>
<?php 
    } 
    }else{
?>
    <h3>NO MORE DATA IN THE DATABASE</h3>
<?php } ?>
</div>


<!-- FETCHING TOTAL ROWS IN DATABASE AND CALCULATING TOTAL PAGE NUMBERS -->
<?php

$sql = "SELECT * from `image_files` where cat_id = $cat_id";
$res = mysqli_query($conn, $sql);
$total_rows = mysqli_num_rows($res);
$total_page = ceil($total_rows / $limit);

$start = ($page - 1);
switch($start){
    case 0:
    $start = 1;
}
if($start == 0){
  $start =1;
}
$end = ($page + 4);
if($end > $total_page){
    $end = $total_page;
}
?>

<ul class="pagination">

        <!-- FIRST AND PREV BUTTON -->
        <?php if($page>1){ ?>
          <a href="?page=1">First</a>
          <a href="?page=<?php echo $page-1 ?>">Prev</a>
        <?php } ?>

        <!-- LOOP FOR PAGE NUMBERS -->
        <?php for($i = $start; $i <= $end; $i++){ 
        $class='';
        if($current_page==$i){
          $class='active';
        ?>

          <a class="<?php echo $class?>" href="javascript:void(0)"><?php echo $i ?></a>

        <?php
        }else{
        ?>

          <a class="<?php echo $class?>" href="?page=<?php echo $i ?>"><?php echo $i ?></a>

        <?php } }?>

        <!-- NEXT AND LAST BUTTON -->
        <?php if(($i-1)>$page){ ?>
          <a href="?page=<?php echo $page+1 ?>">Next</a>
          <a href="?page=<?php echo $total_page;?>">Last</a>
        <?php } ?>

    </ul>

>Solution :

href="?page=<?php echo $i ?>"

Your URL here needs to include the cat_id:

href="?cat_id=<?= $_GET['cat_id'] ?>&page=<?= $i ?>"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading