PHP MySQL创建查询来搜索多个表

时间:2012-03-28 10:54:12

标签: php mysql codeigniter

我有这样的形式:

<form method="POST" action="<?php echo base_url() ?>admin/admin_search">
    <fieldset>
        <label for="nalozi">Nalozi</label><input type="checkbox" name="nalozi" />
        <label for="malio_glasi">Mali oglasi</label><input type="checkbox" name="mali_oglasi" />
        <label for="zute_strane">Zute strane</label><input type="checkbox" name="zute_strane" />
        <label for="berza_rada">Berza rada</label><input type="checkbox" name="berza_rada" />
        <label for="vesti">Vesti</label><input type="checkbox" name="vesti" />
        <label for="event">Dogadjaji</label><input type="checkbox" name="event" />
    </fieldset>      

    <input type="search" name="keyword" id="keyword" />
    <input type="submit" value="Trazi"/>
</form>

和用于搜索的PHP代码:

function admin_search(){

        $keyword = trim($_POST['keyword']);
        $search_explode = explode(" ", $keyword);
        $x = 0;

        $mgs = isset($_POST['mali_oglasi']) ? 1 : "";
        $jbs = isset($_POST['berza_rada']) ? 2 : "";
        $nws = isset($_POST['vesti']) ? 3 : "";
        $ypg = isset($_POST['zute_strane']) ? 4 : "";        

        if($mgs != "" || $jbs != "" || $nws != "" || $ypg != ""){$or = " OR ";}else{$or = "";}
        if($jbs != "" || $nws != "" || $ypg != "" ){$or1 = " OR ";}else{$or1 = "";}
        if($nws != "" || $ypg != "" ){$or2 = " OR ";}else{$or2 = "";}
        if($ypg != "" ){$or3 = " OR ";}else{$or3 = "";}

        $nlz = isset($_POST['nalozi']) ? "person" : "";
        $dgj = isset($_POST['event']) ? "event" : "";

        if($nlz != "" || $dgj != ""){$z = ", "; $or_like = " OR "; }else{$z = " "; $or_like = "";}
        if($dgj != ""){$z1 = ", ";$or_like1 = " OR ";}else{$z1 = " ";$or_like1 = "";}

        if($mgs != "" || $ypg != "" || $jbs != "" || $nws != ""){$gi = "global_info";}else{$gi = "";}

        $sql = "SELECT * FROM ";

        if($gi != ""){$sql .= " $gi $z";}
        if($nlz != ""){$sql .= " $nlz $z1";}
        if($dgj != ""){$sql .= " $dgj";}

        $sql .= " WHERE ";
        if($mgs != ""){$sql .= " global_info.info_type_id = {$mgs} $or1 ";}        
        if($jbs != ""){$sql .= " global_info.info_type_id = {$jbs} $or2 ";}        
        if($nws != ""){$sql .= " global_info.info_type_id = {$nws} $or3 ";}        
        if($ypg != ""){$sql .= " global_info.info_type_id = {$ypg} ";}
        $sql .= " AND ";
        foreach($search_explode as $each){
            $x++;
            if($x == 1){
                if($gi != ""){$sql .= " global_info.name LIKE '%$each%' $or_like ";}   
                if($nlz != ""){$sql .= " $nlz.name LIKE '%$each%'$or_like1 ";}   
                if($dgj != ""){$sql .= " $dgj.name LIKE '%$each%' ";}   

            } else {

                $sql .= " AND global_info.name LIKE '%$each%' ";
            }
        }
        echo $sql;        
        $q = $this->db->query($sql);
        echo $q->num_rows();
        return $q = $q->num_rows() == 0 ? FALSE :  $q->result_array();
    }

此搜索背后的理念 - 我必须能够选择我想要搜索的女巫表,并且关键字的搜索需要适用于任何选择的表。

当选中其中一个复选框时,它工作正常,但是如果检查了两个或更多,并且有多个关键字(目前我只尝试使用两个或更多关键字的global_info表),工作模糊。有时它不起作用,或者如果它正在工作,它会多次给出相同的结果,或除了关键字之外的所有内容。目前我不太明白它为什么会给出它给出的结果。如何使这项工作?

1 个答案:

答案 0 :(得分:2)

尝试将其更改为如下所示:

$tables = array();
if(isset($_POST['mali_oglasi'])){
    $tables['mgs'] = 1;
}
/* 
 repeat for the other tables 
*/

/* Where you're building your WHERE clause, use this instead of the 'OR' logic */
if(!empty($tables)){
    $sql .= 'global_info.info_type_id IN (' . implode(',',$tables) . ')';
}