gpt4 book ai didi

php - 选择大列表(带过滤器)和 PDO

转载 作者:行者123 更新时间:2023-11-29 13:55:54 25 4
gpt4 key购买 nike

我有大量数据(房屋列表),需要能够由用户过滤。卧室的最小/最大数量、最小/最大价格范围、郊区、特性类型。

我已经创建了一个包含过滤选项的数组,但是只有选择所有选项时查询才有效。我需要有能力让它在只选择一个选项的情况下工作。

关于我应该如何解决这个问题有什么想法吗?

$query = $db->prepare("SELECT *, (SELECT MIN(FileName) as FileName 
FROM `images`
WHERE `images`.`PropertyID` = `property`.`PropertyID` order by `images`.`id` asc)
as FileName
FROM `property`
INNER JOIN `features` ON `property`.`PropertyID` = `features`.`PropertyID`
WHERE `property`.`Active` = '1' AND `property`.`homelink` = '0'
AND `property`.`Suburb` = :suburb
AND `features`.`Bedrooms` >= :bedroom_min
AND `features`.`Bedrooms` <= :bedroom_max
AND `property`.`Rent` >= :price_min
AND `property`.`Rent` <= :price_max
AND `property`.`PropertyType` = :property_type
GROUP BY `property`.`propertyid`
ORDER BY `property`.`AdvHeading` = 'LEASED!' ASC, `property`.`rent` DESC");


$vars = array(suburb,bedroom_min,bedroom_max,price_min,price_max,property_type);
foreach($vars as $key) {
${$key} = (isset($_GET[$key]) === true) ? $_GET[$key] : '';
$query->bindValue(':'.$key.'', ${$key}, PDO::PARAM_STR);
}

try {
$query->execute();

$rows = $query->fetchAll(PDO::FETCH_ASSOC);
echo '<pre>', print_r($rows, true), '</pre>';

}
catch(PDOException $e){
die($e->getMessage());
}

最佳答案

您需要动态创建查询。这是recent question where you can see an example

请注意,由于比较运算符不同,您无法将其包装在循环中。最好一一手动制作,就像在引用的问题中一样。

关于php - 选择大列表(带过滤器)和 PDO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15872030/

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