2016-04-27 99 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是否应该在哪里,但它的工作是为了我所需要的。