gpt4 book ai didi

javascript - 使用 jQuery/Ajax 通过下拉菜单过滤 PHP SELECT 查询

转载 作者:行者123 更新时间:2023-11-29 12:04:56 28 4
gpt4 key购买 nike

我正在尝试使用 jQuery/Ajax 和 PHP/MySQL 创建一个过滤器。

我的 HTML 中有 4 个下拉选择,我想实现这样的目标:

当 Ajax 成功发布数据时,我的 PHP 文件检查值是否为空,因此每个下拉列表都会向 SELECT 查询添加“AND”语句,如下所示:

SELECT * from properties WHERE property_id = '$property_id' AND dropdown_value = '$dropdown_value' AND dropdown_value2 = '$dropdown_value2'

所以我可以设置所有变量,只检查发布的值是否为空,如果为空,则根本不添加查询语句。

这可能吗?

谢谢。

编辑

当前代码:

$statements = array();
if (isset($_POST['segmento'])) {
$segmento_query = $_POST['segmento'];
$statements[] = " AND property_details.segmento = '$segmento_query' "; //condition for each property
}
if (isset($_POST['cidade'])) {
$cidade_query = $_POST['cidade'];
$statements[] = " AND property_details.cidade = '$cidade_query' ";
}

$filtraSegmento = "SELECT * FROM properties, property_complements, property_details WHERE property_complements.imovel_id = properties.property_id AND property_details.imovel_id = properties.property_id $statements";

最佳答案

你尝试过吗:

$statements = array();
if (isset($POST['param1']) {
$param1 = $POST['param1'];
$statements[] = " property1 = '$param1' "; //condition for each property
}
if (isset($POST['param2']) {
$param2 = $POST['param2'];
$statements[] = " property2 = '$param2' ";
}

//.....more.....
$sql = "SELECT * FROM tbl WHERE ". implode('AND', $statements);
//do something here

编辑:使用$statements作为字符串

$statements = '';
if (isset($_POST['segmento'])) {
$segmento_query = $_POST['segmento'];
$statements .= " AND property_details.segmento = '$segmento_query' "; //condition for each property
}
if (isset($_POST['cidade'])) {
$cidade_query = $_POST['cidade'];
$statements .= " AND property_details.cidade = '$cidade_query' ";
}

$filtraSegmento = "SELECT * FROM properties, property_complements, property_details WHERE property_complements.imovel_id = properties.property_id AND property_details.imovel_id = properties.property_id $statements";

关于javascript - 使用 jQuery/Ajax 通过下拉菜单过滤 PHP SELECT 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31666304/

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