如果查询列表中有特定项目,我似乎遇到问题。我目前有多个表,主要是用户,字符,系列,项目和&清单。性格与系列链接到项目表,而User&项目在核对表表格中链接。
这种方式的工作方式是,如果按一个按钮按下登录用户不在清单表中的项目,该项目将被添加到清单表中(如果在清单中,则删除按钮将显示)。
我目前的工作如下:
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).
这是我用来获取特定项目信息的查询,如果用户已登录我正在使用codeigniter:
$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 :(得分: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是否应该高于哪里,但它也适用于我需要它。