在zend Framework中构建搜索查询

时间:2011-12-21 02:06:11

标签: php mysql zend-framework

我正在使用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%'";

1 个答案:

答案 0 :(得分:5)

以下是我的示例代码。您的代码应该取决于您的需求,查询可能会与其他表格连接,或者LIKE代替MATCH...AGAINST您可以使用//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; } 来获得更准确的结果。此代码取决于用户输入的每个参数以进行最终查询。

{{1}}
相关问题