gpt4 book ai didi

php - 使用ajax和php按价格排序

转载 作者:行者123 更新时间:2023-11-30 21:30:50 26 4
gpt4 key购买 nike

我使用 ajax 和 php 为我的房地产网站创建了一个产品过滤页面。我现在正在尝试添加按价格排序结果的选项,但我似乎无法将这最后一部分的 sql 代码添加到查询中。 我知道我还应该将值添加为变量,但我尝试过的所有方法要么设置回其他过滤器,要么“ORDER BY”部分根本不包含在查询中。我什至愿意为这个功能创建一个“提交”按钮,但我确实需要它尽可能与其余的过滤合作。我需要一些帮助

<?php


//fetch_data.php

include('database_connection.php');

if(isset($_POST["action"]))
$query = "SELECT * FROM Property Where PropertyID != 0 ";
if(isset($_POST["minimum_price"], $_POST["maximum_price"]) && !empty($_POST["minimum_price"]) && !empty($_POST["maximum_price"]))
{
$query .= "AND Price BETWEEN '".$_POST["minimum_price"]."' AND '".$_POST["maximum_price"]."'";
}

if(isset($_POST["city"]))
{
$city_filter = implode("','", $_POST["city"]);
$query .= "AND City IN('".$city_filter."')";
}
if(isset($_POST["sort"]))
{
$query .= 'AND ORDER BY '.$_REQUEST['sort'];
}


$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total_row = $statement->rowCount();
$output = '';
if($total_row > 0)
{
foreach($result as $row)
{
$output .= '
<div class="col-sm-4 col-lg-3 col-md-3">
<div style="border:1px solid #ccc; border-radius:5px; padding:16px; margin-bottom:16px; height:450px;">
<img src="image/'. $row['Image'] .'" alt="" class="img-responsive" >
<p align="center"><strong><a href="#">'. $row['Name'] .'</a></strong></p>
<h4 style="text-align:center;" class="text-danger" >'. $row['Price'] .'</h4>
<p>Property Type : '. $row['Property_Type'].' <br />
<p>For : '. $row['Listing_Type'].' <br />
<p>Living Area : '. $row['Living_area'].' <br />
<p>City : '. $row['City'].' <br />

</div>

</div>
';
}
}
else
{
$output = '<h3>No Data Found</h3>';
}
echo $output;

?>
<?php

//index.php

include('database_connection.php');

?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>Product filter in php</title>

<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link href = "css/jquery-ui.css" rel = "stylesheet">
<!-- Custom CSS -->
<link href="css/style.css" rel="stylesheet">
</head>

<body>
<!-- Page Content -->
<div class="container">
<div class="row">
<br />
<h2 align="center">Product Filters in PHP</h2>
<br />
<div class="col-md-3">

<select name='sort'>
<option id="ASC" value='ASC'> Price Low to High </option>
<option id="DESC" value='DESC'> Price High to Low </option>
</select>


<div class="list-group">
<h3>Price</h3>
<input type="hidden" id="hidden_minimum_price" value="0" />
<input type="hidden" id="hidden_maximum_price" value="3000000" />
<p id="price_show">1000 - 3000000</p>
<div id="price_range"></div>
</div>


<div class="list-group">
<h3>City</h3>
<?php
$query = "
SELECT DISTINCT(City) FROM Property WHERE Price !=1
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
?>
<div class="list-group-item checkbox">
<label><input type="checkbox" class="common_selector city" value="<?php echo $row['City']; ?>" > <?php echo $row['City']; ?> </label>
</div>
<?php
}
?>
</div>




</div>
<div class="col-md-9">
<br />
<div class="row filter_data">

</div>
</div>
</div>

</div>
<style>
#loading
{
text-align:center;
background: url('loader.gif') no-repeat center;
height: 150px;
}
</style>

<script>
$(document).ready(function(){

filter_data();

function filter_data()
{
$('.filter_data').html('<div id="loading" style="" ></div>');
var action = 'fetch_data';
var minimum_price = $('#hidden_minimum_price').val();
var maximum_price = $('#hidden_maximum_price').val();
var city = get_filter('city');



$.ajax({
url:"fetch_data.php",
method:"POST",
data:{action:action, minimum_price:minimum_price, maximum_price:maximum_price, city:city},
success:function(data){
$('.filter_data').html(data);
}
});
}

function get_filter(class_name)
{
var filter = [];
$('.'+class_name+':checked').each(function(){
filter.push($(this).val());
});
return filter;
}

$('.common_selector').click(function(){
filter_data();
});

$('#price_range').slider({
range:true,
min:1000,
max:3000000,
values:[1000, 3000000],
step:500,
stop:function(event, ui)
{
$('#price_show').html(ui.values[0] + ' - ' + ui.values[1]);
$('#hidden_minimum_price').val(ui.values[0]);
$('#hidden_maximum_price').val(ui.values[1]);
filter_data();
}
});


});
</script>

</body>

</html>

最佳答案

你有这一行是不正确的:

$query .= 'AND ORDER BY '.$_REQUEST['sort'];

应该是:

$query .= ' ORDER BY '.$_REQUEST['sort'];

您还可以添加一个选项来指定顺序 ASC 或 DESC:

$query .= 'AND ORDER BY '.$_REQUEST['sort']. ' {$orderType}';

希望对你有帮助

关于php - 使用ajax和php按价格排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56262058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com