简体   繁体   中英

PHP MySQL create query to search multiple tables

I have form like this:

<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>

and PHP code for searching:

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();
    }

Idea behind this search - I must be able to choose witch tables I want to search and the search by the keyword(s) need to work for any table choosen.

When one of the checkboxes is checked, it is working fine, but if two or more are checked, and if there is more than one keyword (for the moment I am trying just global_info table with two or more keywords), function is working fuzzy. Sometimes it does not work, or if it is working it is giving same results multiple times, or everything except the keyword. At the moment I don't quite understand why it is giving results that it is giving. How to make this work?

Try changing it to read like this:

$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) . ')';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM