Tarek Hassan
Tarek Hassan

Reputation: 23

CodeIgniter - Search results - How to return search results for specific word from multiple tables?

After I get a specific word from form. I want to get results like this word from multiple tables in database.

function search(){
    $search = $this->input->post('searchword');
    $this->db->select('articles.title, articles.post');
    $this->db->select('files.name, files.desc');
    $this->db->select('news.title, news.post');
    $this->db->select('projects.title, projects.desc');
    $this->db->from('articles, files, news, projects');

    // I think problem is here
    $like = array('articles.title' => $search,'articles.post' => $search,
    'files.name' => $search,'files.desc' => $search,
    'news.title' => $search,'news.post' => $search,
    'projects.title' => $search, 'projects.desc' => $search);
    $this->db->like($like);


    return $this->db->get();

}

Upvotes: 0

Views: 775

Answers (1)

Brian
Brian

Reputation: 8626

CI's joining is crap...

I always will create a proper sql statement with joins, and then just execute that.

$query = $this->db->query("BUILD A PROPERLY JOINED QUERY!");

If you're not able to do it yourself... build the query in PHPMyAdmin or MySQL Workbench and make sure you get the results you'd expect.

Also, while I'm not a fan of CI's Active Record, have a look at this post to perhaps tune the join it's creating -http://codeigniter.com/forums/viewthread/93198/

Upvotes: 1

Related Questions