简体   繁体   中英

How to use where condition in pagination using php?

I have a problem in my code, my pagination is not working when i navigate link its generate notice error undefined index: package name. here is my code :

 include("includes/header.php");
    include('php_script/db.php');
    $package_name = $_GET['package_name'];
    if (isset($_GET['pageno'])) {
            $pageno = $_GET['pageno'];

    } else {
         $pageno = 1;
    }
   $no_of_records_per_page = 1;

   $offset = ($pageno-1) * $no_of_records_per_page;

    $result = mysqli_query($con,$total_pages_sql);
   $total_rows = mysqli_fetch_array($result)[0];
  $total_pages = ceil($total_rows / $no_of_records_per_page);
  $sql = "select * from tour_package  where package_name 
 ='".$package_name."' 
order by id desc LIMIT $offset, $no_of_records_per_page ";
 $res_data = mysqli_query($con,$sql);

Here is pagination link :

 <ul class="pagination">
    <li><a href="?pageno=1">First</a></li>
    <li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
    <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=". 
   ($pageno - 1); } ?>">Prev</a> </li>                 
  <li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
  <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "? 
   pageno=".($pageno + 1); } ?>">Next</a></li> 
  <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
  </ul>

Try this

<?php
    include("includes/header.php");
        include('php_script/db.php');

        if(isset($_GET['package_name'])){
            $package_name = $_GET['package_name'];
            $_SESSION['package_name']=$package_name; //First time when you have package_name in url then assign this in session
        }

        if(isset($_SESSION['package_name'])){
            $package_name = $_SESSION['package_name']; //Other times when you have not package_name in url then use this by session
        }

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

        $no_of_records_per_page = 1;

        $offset = ($pageno-1) * $no_of_records_per_page;

        $result = mysqli_query($con,$total_pages_sql);
        $total_rows = mysqli_fetch_array($result)[0];
        $total_pages = ceil($total_rows / $no_of_records_per_page);
        if(isset($package_name)){ // check if you have package_name variable then your query will run
            $sql = "select * from tour_package  where package_name 
            ='".$package_name."' 
            order by id desc LIMIT $offset, $no_of_records_per_page ";
            $res_data = mysqli_query($con,$sql);
        }
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM