gpt4 book ai didi

php搜索帮助

转载 作者:行者123 更新时间:2023-11-29 04:13:24 26 4
gpt4 key购买 nike

以下是我从搜索表单中发布的变量:

$city = $_REQUEST['city'];
$location = $_REQUEST['location'];
$bedrooms = $_REQUEST['noofbedrooms'];
$addeddate = $_REQUEST['addeddate'];
$minprice = $_REQUEST['pricefrom'];
$maxprice = $_REQUEST['priceto'];
$minarea = $_REQUEST['areafrom'];
$maxarea = $_REQUEST['areato'];
$propertytype = $_REQUEST['proptype'];

到目前为止一切顺利。现在我需要针对以下场景的一些好的建议。我领域中的几乎每个元素都是可选的。这意味着我可以在上述变量中​​获取空值。

为上述变量创建 mysql 查询的场景应该是什么。万一我可以为每个场景使用条件。例如

if($city=="")
$query="";
elseif($location=="")
$query="";
and so on....

为此我需要一些专业的方法。

最佳答案

如果所有查询部分都以相同的方式构建:WHERE fieldname='fieldvalue',您可以使用惰性的、基于循环的方法:

$conditions = array();

foreach (array("city", "location", "noofbedrooms") as $field)
// ^ add all fields as needed
{
// Check whether parameter was passed at all
if (!array_key_exists($field, $_POST)) continue;

// Check whether parameter is empty
if (!empty($_POST[$field]))
$conditions[]="`$field` = ".mysql_real_escape_string($_POST[$field]);
// ^ or whatever your database library
// does for escaping

}

$query = "SELECT * from table where ".implode(" AND ", $conditions);

关于php搜索帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4498644/

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