我正在使用 ZendFrameWork 1.11.5 我需要进行多表单搜索查询,就像我有 5 个字段一样
name (text box)
city (dropDown)
zipCode (text box)
type (dropdown)
现在的问题是... 在文本框中,用户可以输入任何内容,可能是正确的信息,也可能是错误的信息。 请建议我如何构建快速查询。我有什么样的选择.. 我也尝试过这个但不起作用..没有给我正确的结果..
Select * from table where type = '%$pType%'
OR sex LIKE '%$sex%'
OR race LIKE '%$race%'
OR kind LIKE '%$kind%'
OR country LIKE '%$Country%'
OR state LIKE '%$statesIDs%'
OR zipcode LIKE '%$zip%'";
最佳答案
这是我为您提供的示例代码。您的代码应该取决于您的需求,查询可能会与另一个表连接,或者您可以使用 MATCH...AGAINST
代替 LIKE
以获得更准确的结果。此代码取决于用户输入的用于进行最终查询的每个参数。
//In DbTable
public function search($params)
{
$query = $this->select()
->from(
array('tbl'=>'table'),
array('name','city','zipcode','type')
);
$query = $this->_makeParams($query,$params);
return $this->fetchAll($query);
}
private function _makeParams($query, $params)
{
$name = isset($params['name']) ? trim($params['name']) : '';
$city = isset($params['city']) ? trim($params['city']) : '';
$zipcode = isset($params['zipcode']) ? trim($params['zipcode']) : '';
$type = isset($params['type']) ? trim($params['type']) : '';
if($name!='')
{
$name = '%'.$this->quote($name).'%';//quote is my own function
$query->where("tbl.name LIKE '?'",$name);
}
if($city!='')
{
$query->where("tbl.city=?",$city);
}
if($zipcode!='')
{
$query->where("tbl.zipcode=?",$zipcode);
}
if($type!='')
{
$query->where("tbl.type=?",$type);
}
return $query;
}
关于php - 在 zend 框架中构建搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8584296/