我正在使用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;
}