Home > OS >  Pagination in PHP not showing data when page is changed
Pagination in PHP not showing data when page is changed

Time:07-18

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.

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 >

<?php
    if(mysqli_num_rows($result)>0){  
    while($row = mysqli_fetch_assoc($result)){
?>
    <div >
        <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 >

        <!-- 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  href="javascript:void(0)"><?php echo $i ?></a>

        <?php
        }else{
        ?>

          <a  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>

CodePudding user response:

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

Your URL here needs to include the cat_id:

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

  •  Tags:  
  • php
  • Related