2012-10-15 51 views
0

從窗體中獲得特定單詞後。我想從數據庫中的多個表中獲得像這個詞這樣的結果。CodeIgniter - 搜索結果 - 如何從多個表中返回特定單詞的搜索結果?

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

} 

回答

1

CI的加入是廢話...

我總是會創造一個適當的SQL語句連接,然後只執行這一點。

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

如果你不能夠做​​自己......建立在phpMyAdmin或MySQL Workbench中的查詢,並確保你得到你所期望的結果。

此外,雖然我不是CI的活動記錄的粉絲,在這個帖子看看或許調整加入它的創造-http://codeigniter.com/forums/viewthread/93198/

+0

+ 1在複雜查詢中使用查詢 – amd

+0

我同意在適當的查詢中創建複雜的連接,但即使您使用CI的連接,我99%的肯定也不會以OP的方式工作。你必須使用$ this-> db-> join,在from中有多個表將不起作用,因爲沒有辦法連接所描述的表。 –

+0

是......因此建議使用一些東西來建立它......我一旦需要除了簡單的選擇以外的任何東西,我總是繞過CI的活動記錄的東西。 – Brian