dpfwhb7470 2011-12-21 02:06
浏览 75
已采纳

在zend Framework中构建搜索查询

i am using ZendFrameWork 1.11.5 i need to make a Multi form search queries like i have 5 fields like

name (text box)
city (dropDown)
zipCode (text box)
type (dropdown)

now the problem is ... in text Box user can enter anything it could be correct info or not.. kindly suggest me how to build a fast query.what kind of options i have.. also i tried this but not working.. not giving me correct result..

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条回答 默认 最新

  • duanlidi1051 2011-12-21 04:57
    关注

    Here is my sample code for you. Your code should depends on your needs, query may joins with another table, or instead of LIKE you can use MATCH...AGAINST for more accuracy result. This code depends on each parameter that user enter for making a final query.

    //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;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站