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).

如果用户登录我使用 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) 之外,它们与上述相同。

4

1 回答 1

0

好像我找到了解决办法。我移动了join下的where item ID,将or_where改为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 是否应该在 where 之上,但它也可以满足我的需要。

于 2016-04-27T22:35:07.007 回答