【问题标题】:How to get result parent name of category from category id using codeigniter如何使用codeigniter从类别ID中获取类别的结果父名称
【发布时间】:2016-08-25 07:45:53
【问题描述】:

请帮助我,如何获取类别的父名称。我有这样的桌子。

table Category
+-------------+-----------------+-----------------+---------------+
| category_id | category_name   | category_parent | category_slug |
+-------------+-----------------+-----------------+---------------+
|           7 | Framework CSS   |               0 |               |
|           9 | Bootstrap       |               7 |               |
|          10 | Material Design |               7 |               |
+-------------+-----------------+-----------------+---------------+

table relationship
+-----------------+---------+-------------+
| relationship_id | post_id | category_id |
+-----------------+---------+-------------+
|              35 |      59 |           9 |
+-----------------+---------+-------------+

我使用 codeigniter,并且我有一个获取帖子和类别的功能。

public function post_detail($tableName, $where = '')
{
    $post_title = $this->uri->segment(3);

    $query = $this->db->query("SELECT p.*, r.*, c.* FROM tbl_relationship AS r 
        INNER JOIN tbl_posts AS p ON r.post_id = p.post_id
        INNER JOIN tbl_category AS c ON r.category_id = c.category_id
        WHERE p.post_name = '$post_title'
    ");
    return $query->result();    
}

函数结果:(7 和引导程序)。如何得到 7. 的父名称?

【问题讨论】:

标签: sql codeigniter


【解决方案1】:

试试这个:

public function post_detail($tableName, $where = '')
{
    $post_title = $this->uri->segment(3);

    $query = $this->db->query("SELECT p.*, r.*, c.*, c1.category_name as parent_name FROM tbl_relationship AS r 
        INNER JOIN tbl_posts AS p ON r.post_id = p.post_id
        INNER JOIN tbl_category AS c ON r.category_id = c.category_id
        INNER JOIN tbl_category AS c1 ON c.category_parent = c1.category_id
        WHERE p.post_name = '$post_title'
    ");
    return $query->result();    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-24
    • 2016-06-25
    • 2013-05-21
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多