如何使用php在分页中使用where条件?

qvtsj1bj  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(259)

我的代码中有一个问题,当我导航链接时,我的分页不起作用,它的生成通知错误未定义索引:package name。这是我的密码:

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);

以下是分页链接:

<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>
13z8s7eq

13z8s7eq1#

试试这个

<?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);
        }
    ?>

相关问题