我正在尝试使用像这样的SELECT下拉价格范围:
<select name="valor">
<option value="" selected="selected">Faixa de Valor</option>
<option value="100000.00 , 200000.00">De R$ 100 a 200 mil</option>
<option value="200000.00 , 500000.00">De R$ 200 a 500 mil</option>
<option value="500000.00 , 1000000.00">De R$ 500 a 1 milhão</option>
<option value="1000000.00 , 5000000.00">De R$ 1 a 5 milhões</option>
<option value="5000000.00">Acima de 5 Milhões</option>
</select>
我的PHP语句是:
$valor_query = $_POST['valor'];
$values = explode(' , ', $valor_query);
$valor1 = $values[0];
$valor2 = $values[1];
$statements .= " AND ( (property_selling.valor >= '$valor1' AND property_selling.valor >= '$valor2') OR (property_renting.valor >= '$valor1' AND property_renting.valor >= '$valor2') ) ";
但它不起作用,我有两个不同的表,一个用于销售,另一个用于租赁,我将此语句添加为这样的变量:
$filtraSegmento = "SELECT * FROM properties, property_complements,
property_details, property_selling, property_renting
WHERE property_complements.imovel_id = properties.property_id
AND property_details.imovel_id = properties.property_id
AND (property_renting.imovel_id = properties.property_id
OR property_selling.imovel_id = properties.property_id)
AND properties.property_status = 'Aprovado' $statements
GROUP BY properties.property_id";
任何提示?
修改
正如@Strawberry和@Sean所建议的那样,我使用LEFT JOIN重新创建了查询,现在它看起来像:
$filtraSegmento = "
SELECT p.*, c.*, d.*, s.*, r.*
FROM properties p
LEFT
JOIN property_complements c
ON c.imovel_id = p.property_id
LEFT
JOIN property_details d
ON d.imovel_id = p.property_id
LEFT
JOIN property_selling s
ON s.imovel_id = p.property_id
LEFT
JOIN property_renting r
ON r.imovel_id = p.property_id
WHERE $statements
";
现在效果很好,但我的问题是,我使用它作为过滤器,而用户继续过滤,变量$statements
不断添加新条件。所以我有一个数组格式,如:
$statements = '';
if (isset($_POST['segmento']) && !empty($_POST['segmento'])) {
$segmento_query = $_POST['segmento'];
$statements .= " detail.segmento = '$segmento_query' "; //condition for each property
}
if (isset($_POST['cidade']) && !empty($_POST['cidade'])) {
$cidade_query = $_POST['cidade'];
$statements .= " detail.cidade = '$cidade_query '";
}
if (isset($_POST['tipo_imovel']) && !empty($_POST['tipo_imovel'])) {
$tipo_query = $_POST['tipo_imovel'];
$statements .= " detail.tipo_imovel = '$tipo_query '";
}
if (isset($_POST['local']) && !empty($_POST['local'])) {
$local_query = $_POST['local'];
$statements .= " property.property_category = '$local_query '";
}
if (isset($_POST['valor']) && !empty($_POST['valor'])) {
$valor_query = $_POST['valor'];
$values = explode(',', $valor_query);
$valor1 = $values[0];
$valor2 = $values[1];
$statements .= " selling.valor BETWEEN '$valor1' AND '$valor2' OR renting.valor BETWEEN '$valor1' AND '$valor2' ";
}
如何将其作为新条件添加到查询中?
答案 0 :(得分:0)
您可以尝试使用嵌套条件运算符,而不是爆炸提交的值。使用1,2,3,..等值作为选项,你可以在php中做到这一点。
$r = $_POST["valor"]
".(
empty($r) ? "" : ( ($r == "0") ? "" :
( ($r == "1") ? "and rent <= 5000" :
( ($r == "2") ? "and rent between 5000 and 10000" : "and rent > 10000" )
)
)
)."