2016-04-27 97 views
0

我似乎有一個查詢顯示特定項目,如果它是在清單表中的問題。我目前有多個表,其中主要是用戶,字符,系列,項目&清單。字符&系列鏈接到項目表中,而用戶&項目鏈接在清單表中。選擇查看項目返回空(檢查錶鏈接表)

這種方式的工作方式是,如果一個按鈕被一個登錄的用戶不在清單表中的項目按下,這個項目將被添加到清單表格中(如果在清單中有,刪除按鈕將被顯示)。

我現在有工作如下:

  • 如果用戶沒有在和主頁上記錄:顯示有 X
  • 所有項目如果用戶在與主記錄頁面:顯示所有不在
    的項目 清單表中有一個X和清單中的所有項目表
    請勾選。
  • 如果用戶沒有在點擊登錄查看項目的詳細信息:
    顯示有關該項目的詳細信息與X(單件商品頁面)

What I am trying to achieve now is if a user is logged in and clicks view more details, to show the individual item page with a X or Tick (Depending if in checklist table). This is kinda working as items which are in the checklist table can be selected to view the individual item page when logged in. However if the item is not in the checklist table and the user is logged in, nothing is returned back. I am at a total loss how to resolve this and am trying to avoid copying all the items into the checklist table with an additional field (Y/N).

這是我的查詢如果使用一個用戶登錄裏面我是用笨來獲取特定項目的信息:

$this->db->select('item.item_id AS item_id, item.item_image AS item_image, line.line_name AS line_name, series.series_name AS series_name, character.character_name AS character_name, checklist.checklist_id AS checklist_id'); 
$this->db->from('item'); 
$this->db->join('line', 'item.line_id = line.line_id', 'left'); 
$this->db->join('series', 'item.series_id = series.series_id', 'left'); 
$this->db->join('character', 'item.character_id = character.character_id', 'left'); 
$this->db->join('checklist', 'checklist.item_id = item.item_id', 'left'); 
$this->db->where('checklist.users_id', $user_id); 
$this->db->or_where('checklist.users_id IS NULL'); // Also tried without this line 
$this->db->where('item.item_id', $item_id); 
$query = $this->db->get(); 
return $query->row_array(); 

其他三個查詢正在運行,如任何幫助將是真正偉大傾向於,與上面相同除了返回($ query-> result_array)&添加了or_where(checklist.users_id IS NULL)。

+0

你能編輯和突出你的問題嗎? – Kisaragi

+0

Hi @Kisaragi,更新了突出顯示的問題。 – Jake

回答

0

似乎我找到了解決辦法。我移動了連接下的項目ID的位置,將or_where更改爲where,以及將or_where更改爲何處。所以工作代碼如下:

$this->db->select('item.item_id AS item_id, item.item_image AS item_image, line.line_name AS line_name, series.series_name AS series_name, character.character_name AS character_name, checklist.checklist_id AS checklist_id'); 
$this->db->from('item'); 
$this->db->join('line', 'item.line_id = line.line_id', 'left'); 
$this->db->join('series', 'item.series_id = series.series_id', 'left'); 
$this->db->join('character', 'item.character_id = character.character_id', 'left'); 
$this->db->join('checklist', 'checklist.item_id = item.item_id', 'left'); 
$this->db->where('item.item_id', $item_id);  
$this->db->or_where('checklist.users_id', $user_id); 
$this->db->where('checklist.users_id IS NULL');  
$query = $this->db->get(); 
return $query->row_array(); 

不完全確定or_where是否應該在哪裏,但它的工作是爲了我所需要的。