php - 选择查看项目返回 Null( list 链接表)

标签 php mysql codeigniter linktable

我似乎在查询显示特定项目(如果它位于 list 表中)时遇到问题。我目前有多个表,主要是用户、角色、系列、项目和 list 。角色和系列链接到项目表中,而用户和项目链接到 list 表中。

其工作方式是,如果登录用户按下不在 list 表中的项目按钮,则该项目将被添加到 list 表中(如果在 list 中可用,则删除按钮将被添加到 list 表中)显示)。

我目前正在做的工作如下:

  • 如果用户未登录并在主页上:显示带有 X
  • 如果用户已登录并在主页上:显示所有未登录的项目
    带有 X 的 list 表以及带有 X 的 list 表中的所有项目
    勾选。
  • 如果用户未登录,请点击查看有关项目的更多详细信息:
    使用 X 显示该商品的更多详细信息(单个商品页面)

我现在想要实现的是,如果用户登录并单击“查看更多详细信息”,则显示带有 X 或勾号的单个项目页面(取决于是否在 list 表中)。这有点像在登录时可以选择 list 表中的项目来查看单个项目页面。但是,如果该项目不在 list 表中并且用户已登录,则不会返回任何内容。我完全不知道如何解决这个问题,并且试图避免将所有项目复制到带有附加字段(Y/N)的 list 表中。

如果用户登录时我正在使用 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();

任何帮助都会非常有用,因为其他三个查询正在按预期运行,除了 return ($query->result_array) 和添加的 or_where (checklist.users_id IS NULL) 之外,它们与上面的相同。

最佳答案

看来我已经找到了解决办法。我将 where 项目 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,但它也能满足我的需要。

关于php - 选择查看项目返回 Null( list 链接表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36900683/

相关文章:

php - 为什么在 PHP 变量前使用@?

PHP更改linux用户密码(passwd)

javascript - AJAX 表单 Post PreventDefault 不起作用

mysql - 如何将sql请求与另一个表中的数据结合起来

mysql - 按多个表mysql中的重复项数量排序

php - 如何从我的数据库填充的表中进行搜索

html - Codeigniter 电子邮件 - 样式标签/CSS 不起作用

php - 在 Mysql 或缓存中存储数据与在 PHP 中计算

php - 如何不从 SQL 中的相邻表中选择另一个表中列出的 id?

php - CodeIgniter 存储过程的多个结果