I've done everything correctly, but I do not know how to fix it I think the problem in models after modified please help I want to work for three of the tables. Is this true or use the join?
Note: tables structures are identical
The code was working correctly with me like this:
$this->db->select("*");
$this->db->from("posts");
if($query != '')
{
$this->db->like('title', $query);
}
return $this->db->get();
}
The code does not work
model:
function fetch_data($query){
$data = $this->db->query("
SELECT * FROM (
SELECT id, title, keywords, 'posts' AS type FROM posts
UNION
SELECT id, title, keywords, 'android' AS type FROM android
UNION
SELECT id, title, keywords, 'mac' AS type FROM mac
) t WHERE title like '%$query%' OR keywords LIKE '%$query%'
");
}
I suspect the issue if that you are not returning the result from the db object which simply requires adding return $data->result;
at the end of the function. However another method you can try is using the get_compiled_select() method as shown in the codeigniter docs.
The function edit using this as a rough draft can be seen below. By the sounds of it, your database structure should change to eliminate the need for such types of queries, however it's hard to make a recommendation based on the lack of transparency on your database structure.
function fetch_data($query)
{
$this->db->select("id, title, keywords, 'posts' AS type");
$this->db->from("posts");
$query1 = $this->db->get_compiled_select('posts');
$this->db->select("id, title, keywords, 'android' AS type");
$this->db->from("android");
$query2 = $this->db->get_compiled_select('android');
$this->db->select("id, title, keywords, 'mac' AS type");
$this->db->from("mac");
$query3 = $this->db->get_compiled_select('mac');
$data = $this->db->query('SELECT * FROM (' . $query1 . ' UNION ' . $query2 . ' UNION ' . $query3 . ')' . "t WHERE t.title like '%$query%' OR t.keywords LIKE '%$query%'");
return $data->result();
}