Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ajax和PHP进行分页以使用过滤器_Php_Jquery - Fatal编程技术网

如何使用ajax和PHP进行分页以使用过滤器

如何使用ajax和PHP进行分页以使用过滤器,php,jquery,Php,Jquery,我允许用户从下拉列表和滑块中筛选数据。我让过滤部分正常工作并显示数据,问题是当我尝试对数据分页时。我使用AJAX将数据从表单传递到PHP过滤脚本。是否可以将这些值存储在$\会话中,然后将这些值传递回筛选器,以便在以下页面上显示正确的值 下面是我目前正在使用的一些代码 index.php- 我正在使用自定义select类获取每个表单select值的值,并将它们发送到jquery filterData函数 <div class="form-group" style="padding:5px;c

我允许用户从下拉列表和滑块中筛选数据。我让过滤部分正常工作并显示数据,问题是当我尝试对数据分页时。我使用AJAX将数据从表单传递到PHP过滤脚本。是否可以将这些值存储在$\会话中,然后将这些值传递回筛选器,以便在以下页面上显示正确的值

下面是我目前正在使用的一些代码

index.php- 我正在使用自定义select类获取每个表单select值的值,并将它们发送到jquery filterData函数

<div class="form-group" style="padding:5px;color:#333;">
  <label for="citySelect">LOCATION</label>
  <select class="form-control custom-select citySelect" id="citySelect" data-id="<?php echo $user_id; ?>">
    <optgroup label="France">
        <option disabled selected>Select A City</option>
        <option value="paris">Paris</option>
    </optgroup>
  </select>
</div>
<div class="col-xs-6 col-sm-6">
    <div class="form-group">
        <label for="keyWordSearch">KEYWORDS</label>
        <div class="iconContainer">
            <input type="text" class="form-control keyWordSearchStyle custom-select" name="keyWordSearch" id="keyWordSearch" placeholder="keyword search...">
            <i class="fa fa-search iconPosition"></i>
        </div>
    </div>
</div>  
然后在jquery函数中,我将这些值发送到filterData.php页面

function filterOptionSelect(city, page){
event.preventDefault();

var city = city;
var rate = $(".rateSelect").val();
var lowRange = $(".min").val();
var highRange = $(".max").val();
var keyword = $("#keyWordSearch").val();

$.ajax({
    type: "POST",
    url: "filterData.php",
    data: {
        "city": city,
        "rate": rate,
        "lowRange": lowRange,
        "highRange": highRange,
        "keyword": keyword,
    },
    dataType: "html",
    beforeSend: function(){
        $('.screenLoader').show();
    },
    success: function(response){
        var response = $.trim(response);
        if(response){
            setTimeout(function(){
                $('.screenLoader').fadeOut(500);
                $("#displayPage").html(response).fadeIn(2500);
            }, 800);                
          }
      }
  });
  $('.screenLoader').fadeOut(500);
 }
最后在filterData.php页面中,我获取所有变量并查询它们。问题是,当我尝试对数据进行分页时,我不太确定如何使用新的过滤器选项更新查询。如何使用$\u会话并将这些值返回到filterData.php以更新查询

filterData.php

这是我用来过滤数据的文件。到目前为止,除了我的分页外,它似乎工作得很好。它只是在下一页返回值,就像我没有使用任何过滤器一样

$city = '';
$rate = '';
$lowAge = '';
$highAge = '';
$keyword = '';

$records_per_page = 6;
$starting_position = 0; 

if(isset($_POST["page"])){
     $starting_position = ($_POST["page"]-1) * $records_per_page;
}else{
    $starting_position = 1;
}
if(isset($_SESSION['city'])){
    $city = $filter->filter($_SESSION['city']); 
}else{
    if(isset($_POST['city'])){
        $city = $filter->filter($_POST['city']);
        $_SESSION['city'] = $city;
    }
}

if(isset($_SESSION['rate'])){
    $rate = $filter->filter($_SESSION['rate']);
}else{
    if(isset($_POST['rate'])){
        $rate = $filter->filter($_POST['rate']);
        $_SESSION['rate'] = $rate;
    }
}
if(isset($_SESSION['lowRange'])){
    $lowAge = $filter->filter($_SESSION['lowRange']);
}else{
    if(isset($_POST['lowRange'])){
        $lowAge = $filter->filter($_POST['lowRange']);
        $_SESSION['lowRange'] = $lowAge;
    }else{
        $lowAge == '18';
    }
}
if(isset($_SESSION['highRange'])){
    $highAge = $filter->filter($_SESSION['highRange']);
}else{
    if(isset($_POST['highRange'])){
        $highAge = $filter->filter($_POST['highRange']);
        $_SESSION['highRange'] = $highAge;
    }else{
        $highAge == '60';
    }
}
if(isset($_SESSION['keyword'])){
    $keyword = $filter->filter($_SESSION['keyword']);
}else{
    if(isset($_POST['keyword'])){
        $keyword = $filter->filter($_POST['keyword']);
        $_SESSION['keyword'] = $keyword;
    }
}


$query = "SELECT
        ads.ad_id,
        ads.ad_age,
        ads.ad_city,
        ads.ad_rate,
        ads.ad_keyword,
        ads.ad_approved,
        ads.ad_date,
        users.id,
        users.username

        FROM
            ads
        INNER JOIN
            users
        ON
            ads.ad_user = users.user_id 
        WHERE
            ads.ad_approved = '0'
    ";


if(isset($city) && !empty($city)){
    $query .= " AND ads.ad_city = :city";
}else{
    $city = '';
    $query .= " AND ads.ad_city != :city";
}

if(isset($rate) && !empty($rate)){
    if($rate == "all"){
        $query .= " AND ads.ad_rate >= 0";
    }
    else if($rate == "rate1"){
        $query .= " AND ads.ad_rate BETWEEN 0 AND 150";
    }
    else if($rate == "rate2"){
        $query .= " AND ads.ad_rate BETWEEN 151 AND 250";
    }
    else if($rate == "rate3"){
        $query .= " AND ads.ad_rate BETWEEN 251 AND 350";
    }
    else if($rate == "rate4"){
        $query .= " AND ads.ad_rate BETWEEN 351 AND 5000";
    }else{
        $query .= " AND ads.ad_rate >= 0";
    }
}

if(isset($lowAge, $highAge) && !empty($lowAge) && !empty($highAge)){
    $lowAge = $lowAge;
    $highAge = $highAge;
    $query .= " AND ads.ad_age BETWEEN :lowAge AND :highAge";
}else{
    $lowAge = 18;
    $highAge = 60;
    $query .= " AND ads.ad_age BETWEEN :lowAge AND :highAge";
}
if(isset($keyword) && !empty($keyword)){
    $query .= " AND ads.ad_content LIKE :keyword";
}else{
    $keyword = '';
    $query .= " AND ads.ad_content != :keyword";
}

$query .= " ORDER BY ads.ad_date DESC";
$queryPage = $query." LIMIT $starting_position, $records_per_page";

$stmt = $conn->prepare($queryPage);
$stmt->bindParam(":city", $city);
$stmt->bindParam(":lowAge", $lowAge);
$stmt->bindParam(":highAge", $highAge);
$stmt->bindParam(":keyword", $keyword);
$stmt->execute();
$results = $stmt->fetchAll(); 
这是分页部分

$stmtPage = $conn->prepare($queryPage);
$stmtPage->bindParam(":city", $city);
$stmtPage->bindParam(":lowAge", $lowAge);
$stmtPage->bindParam(":highAge", $highAge);
$stmtPage->bindParam(":keyword", $keyword);
$stmtPage->execute();
$resultsPagination = $stmtPage->fetchAll();

$total_no_of_records = $stmtPage->rowCount();

if($stmtPage->rowCount() > 0){
    $current_page = 1;
    $total_no_of_pages = ceil($total_no_of_records/$records_per_page);  

    if(isset($_POST["page"])){
       $current_page = $_POST["page"];
    }   

    ?><div class="row text-center"><ul class='pagination'><?php

    if($current_page != 1){
        $previous = $current_page - 1;            
        echo "<li><a class='boldTextLink paginationLinkPrev panelBGSHSM' data-prev-id='".$previous."'>PREVIOUS PAGE</a></li>";          
    }

    if($current_page < $total_no_of_pages){
        $next = $current_page + 1;
        echo "<li><a class='boldTextLink paginationLink panelBGSHSM' data-next-id='".$next."'>NEXT PAGE</a></li>";
    }
    ?></ul></div><?php
    $recordsTotal = $total_no_of_records;
}
$stmtPage=$conn->prepare($queryPage);
$stmtPage->bindParam(“:city”,$city);
$stmtPage->bindParam(“:lowAge”,$lowAge);
$stmtPage->bindParam(“:highAge”,$highAge);
$stmtPage->bindParam(“:关键字“,$keyword”);
$stmtPage->execute();
$resultsPagination=$stmtPage->fetchAll();
$total_no_of_records=$stmtPage->rowCount();
如果($stmtPage->rowCount()>0){
$current_page=1;
$total_no_of_pages=ceil($total_no_of_records/$records_per_pages);
如果(isset($_POST[“page”])){
$current_page=$_POST[“page”];
}   

?>
    您可以使用隐藏字段并在隐藏字段中存储筛选值,并且在分页时,您可以从隐藏字段中获取值,并传递到分页代码

    例如: 我们有两个隐藏字段

    <input type="hidden" id="hidden_city" value="">
    <input type="hidden" id="hidden_state" value="">
    

    然后将城市、州转到分页。

    检查此链接,可能会对您有所帮助,-抱歉,我不太清楚您面临的错误是什么…请始终询问确切的错误!不要发布完整的代码。您好@BloodyProgrammer,我现在正在分解该代码,看看如何将其合并到我自己的项目中。谢谢您的回复。t请告诉我,当您单击分页按钮时,会出现什么确切的错误!我遇到的问题是,将筛选数据中的值获取到分页查询中,这样当用户单击下一页链接时,筛选的值将传递到分页查询中。现在,用户可以筛选数据,但当他们单击下一页时,它将ont根据所选筛选器返回所需的值。它只是将查询重置为默认值。
    <input type="hidden" id="hidden_city" value="">
    <input type="hidden" id="hidden_state" value="">
    
    <input type="hidden" id="hidden_city" value="city1">
    <input type="hidden" id="hidden_state" value="state1">
    
     var city=$("#hidden_city").val();
     var state=$("#hidden_state").val();