phpmysqlcodeignitersql-likeunion-all

How to use CodeIgniter to perform a multi-keyword search across multiple tables and identify the source table in the result set


I have code like this:

function search_keyword()
{
    $keyword = trim($_POST['keyword']);
    $search_explode = explode(" ", $keyword);
    $x = 0;
        
    $sql = " ( SELECT name AS type FROM global_info WHERE ";
    foreach ($search_explode as $each) {
        $x++;
        if ($x == 1) {
            $sql .= " name LIKE '%$each%'";
        } else {
            $sql .= " name LIKE '%$each%' ";
        }
    }   
    $sql .= " ) UNION ALL "; 
          
    $sql .= " ( SELECT name AS type FROM person WHERE ";
    foreach ($search_explode as $each) {
        $x++;
        if ($x == 1) {
            $sql .= " name LIKE '%$each%'";
        } else {
            $sql .= " name LIKE '%$each%' ";
        }
    }
    $sql .= " ) UNION ALL ";
          
    $sql .= "( SELECT name AS type FROM event WHERE ";
    foreach ($search_explode as $each) {
        $x++;
        if ($x == 1) {
           $sql .= " name LIKE '%$each%'";
        } else { 
           $sql .= " name LIKE '%$each%' ";
        }
    }
    $sql .= " ) ";
        
    $q = $this->db->query($sql);
    return $q = $q->num_rows() == 0 ? FALSE :  $q->result();
}

The function is working. What I don't know is how to know which data is from which table, and then to show the results on the page?


Solution

  • Perhaps, adding a field to the results of each SELECT.

    function search_keyword(){
            $keyword = trim($_POST['keyword']);
            $search_explode = explode(" ", $keyword);
            $x = 0;
    
            $sql = " ( SELECT name AS type, \"table_global_info\" as mytable FROM global_info WHERE ";
            foreach($search_explode as $each){
                $x++;
                if($x == 1){
                   $sql .= " name LIKE '%$each%'";}                          
                else {
    
                    $sql .= " name LIKE '%$each%' ";
                }
            }
    
              $sql .= " ) UNION ALL "; 
    
              $sql .= " ( SELECT name AS type, \"person\" as mytable FROM person WHERE ";
            foreach($search_explode as $each){
                $x++;
                if($x == 1){
                   $sql .= " name LIKE '%$each%'";}                          
                else {
    
                    $sql .= " name LIKE '%$each%' ";
                }
            }
    
              $sql .= " ) UNION ALL ";
    
              $sql .= "( SELECT name AS type, \"event\" as mytable FROM event WHERE ";
            foreach($search_explode as $each){
                $x++;
                if($x == 1){
                   $sql .= " name LIKE '%$each%'";}                          
                else {
    
                    $sql .= " name LIKE '%$each%' ";
                }
            }
    
            $sql .= " ) ";
    
            $q = $this->db->query($sql);
             return $q = $q->num_rows() == 0 ? FALSE :  $q->result();
        }
    

    Regards!