【问题标题】:SQL query not displaying the product in each sub categorySQL 查询不显示每个子类别中的产品
【发布时间】:2019-08-26 22:28:28
【问题描述】:

我试图在每个类别上显示一个产品,从顶级父类别一直到该类别。我有这样的数据库设置:

CATEGORY TABLE:
id  |  parent_id   |  root_cat   |  name
5       37              0            bulbs
45      5               0            standard auxiliary
289     45              5            standard
297     289             5            5W

现在这是产品表:

id    |   name    |   cat
470       bulb 5w      297

这种情况正在发生,当我单击灯泡类别时,我看到的产品很好,但是当我单击子类别标准辅助时,我看不到产品。但是,如果我达到标准的另一个级别,我可以再次看到该产品,并且如果我点击 5W 猫,我也可以看到该产品。所以问题只在标准辅助类上。

这是我用来检索数据的方法: $table_2 是产品表,$table 是类别表。

public function getProductsWhereParentIdis($cat) {
        $check = $this->checkCategoryChildren($cat);
        if($check) {
            $query = "
            SELECT * 
            FROM categories 
            INNER JOIN products 
            ON products.category = categories.id 
            WHERE categories.id = '".$this->db->escape($cat)."' 
            OR categories.parent_id = '".$this->db->escape($cat)."' 
            OR categories.root_category = '".$this->db->escape($cat)."'";   

            return $this->db->fetchAll($query);
        } else {
            $query = "SELECT * FROM {$this->table_2} WHERE category = '".$this->db->escape($cat)."'";

            return $this->db->fetchAll($query);
        }


    }

而且这个方法只是检查一个产品是否有父级:

public function checkCategoryChildren($id = null) {
        if(!empty($id)) {
            $query = "SELECT * FROM {$this->table} WHERE parent_id = '".$this->db->escape($id)."' ORDER BY view_order ASC";

            return $this->db->fetchAll($query);
        }
    }

这是 fetchAll 方法:

public function query($query) {
        $this->last_query = $query;
        $result = mysqli_query($this->connection, $query);
        $this->confirm_query($result);
        return $result;
    }

    public function confirm_query($result) {
        if(!$result) {
            $output  = "Database query failed<br />";
            $output .= "Last SQL query: ". $this->last_query;
            die($output);
        } else {
            $this->affected_rows = mysqli_affected_rows($this->connection);
        }
    }

    public function fetchAll($query) {
        $result = $this->query($query);
        $output = array();
        while($row = mysqli_fetch_assoc($result)) {
            $output[] = $row;
        }
        mysqli_free_result($result);
        return $output;
    }

真的很期待任何人的建议,为什么我可以在除一个之外的所有类别中看到该产品,谢谢。

【问题讨论】:

  • 有多少种组合?
  • 我尝试了 3 次获取 id、parent_id 和另一个我没有输入但仍然没有得到预期效果的 root_parent
  • 别担心 Dharman,我会转义每个查询,但您只是看不到它,因为我调用了不同类的方法,我在每个输入上调用的转义方法使我摆脱了任何 sql 注入
  • @Strawberry 我从语句中删除了内部,但它没有不同的效果
  • 不是转义,问题是手动转义。使用带有占位符值的准备好的语句。这意味着更少的代码和更少的问题作为奖励。

标签: php mysql


【解决方案1】:

这是我认为您正在寻找的解决方案 主要问题是您只看到仅上一级或下一级的类别数据

我还在代码中提到了一些 cmets,请检查一下。 而且我也不知道您的fetchAll() 函数如何返回数据。 例如。 objectarray

编辑:所以在这段代码中fetchAll()函数以数组格式返回数据,因为你使用了mysqli_fetch_assoc()

public function getProductsWhereParentIdis($cat)
{
    $check = $this->checkCategories($cat);
    if (!empty($check)) {
        $query = "
        SELECT * 
        FROM categories 
        INNER JOIN products 
        ON products.category = categories.id 
        WHERE categories.id in (".(implode(",", $check)).")";
        return $this->db->fetchAll($query);
    } else {
        $query = "SELECT * FROM {$this->table_2} WHERE category = '".$this->db->escape($cat)."'";

        return $this->db->fetchAll($query);
    }
}

public function checkCategories($cat_id)
{
    $query = "SELECT * FROM {$this->table} WHERE id = '".$this->db->escape($cat_id)."' ORDER BY view_order ASC";
    $catObj = $this->db->fetchAll($query);
    $childs = $this->getChildCategoryIds($cat_id);
    $childs[] = $cat_id;
    // you can remove the parents from here if you don't want to get all
    // products which have parents categories associated in it.
    // eg. while user click on standard then if you don't want to get the products which
    // is assigned with bulbs or standard auxiliary then return only childs.
    $parents = $this->getParentCategoryIds($catObj[0]['parent_id']);
    return array_merge($childs, $parents);
}

public function getChildCategoryIds($cat_id)
{
    $cat_array = [];
    $q = "SELECT * FROM {$this->table} WHERE parent_id = '".$this->db->escape($cat_id)."' ORDER BY view_order ASC";
    $result = $this->db->fetchAll($q);
    foreach ($result as $value) {
        $tmp_array = $this->getChildCategoryIds($value['id']);
        $cat_array[] = $value['id'];
        if (!empty($tmp_array)) {
            $cat_array = array_merge($cat_array, $tmp_array);
        }
    }
    return $cat_array;
}

public function getParentCategoryIds($cat_id)
{
    $cat_array = [];
    $q = "SELECT * FROM {$this->table} WHERE id = '".$this->db->escape($cat_id)."' ORDER BY view_order ASC";
    $result = $this->db->fetchAll($q);
    if (!empty($result[0]['parent_id'])) {
        $tmp_array = $this->getParentCategoryIds($result[0]['parent_id']);
        $cat_array[] = $result[0]['parent_id'];
        if (!empty($tmp_array)) {
            $cat_array = array_merge($cat_array, $tmp_array);
        }
    }

    return $cat_array;
}

【讨论】:

  • 我也包含了 fetchAll 方法供您查看,我拿走了您的代码并对其进行了测试,但不幸的是它不起作用。在 checkCategories 方法中,您编写了 $q ,我将其更改为 $query 以匹配 $catObj 变量,因为它引发了错误
  • 那么所有对结果的访问就像$result[0]['parent_id']让我编辑答案
  • @Blu3 立即查看
  • 不,现在它根本无法正常工作。请记住,当我单击灯泡时,我会看到最后一个子类别中的产品以及在顶部主类别下分配给其他类别的产品,唯一的问题是在第二层,这里让我添加一些照片供您查看我的代码的结果
  • 完成了,刚刚添加了,如果你完全看懂了告诉我
【解决方案2】:

FWIW,我觉得这更容易阅读......

SELECT c.olumms
     , i.actually
     , w.ant
  FROM categories c
  JOIN products p
    ON p.category = c.id 
 WHERE :mystring IN (c.root_category,c.parent_id,c.id);

【讨论】:

  • 什么是 :mystring IN.... 我不明白我应该如何输入这个
  • 查看准备好的和绑定的查询的占位符
  • 但这不起作用,因为当我像法币一样进入第一个潜艇时,它不会显示任何内容,除非我去掀背车或轿车,你明白吗?
  • 这又回到了我的第一条评论
  • 它是这样的:自动 -> 灯泡 -> 标准辅助 -> 标准 -> 1.2W
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
  • 2018-01-17
  • 1970-01-01
  • 2018-11-13
相关资源
最近更新 更多