具有所有子类别的数据MYSQL查询到Codeigniter活动记录的父类别

时间:2013-01-31 09:03:56

标签: mysql codeigniter activerecord

大家好,因为这个查询我可以在MYSQL中工作,但我想要在Codeigniter Active记录中进行转换。

基本上我有3张桌子

  • 供应商(使用intCat,intCity,*)
  • 类别(使用intId,strCategory,intParent)
  • tblcitylist(使用city_id,city_name)

查询:

SELECT category.strCategory AS strParent, Query3. *  FROM (

SELECT tblcitylist.city_name AS strCity, Query2. *  FROM (

SELECT Query1.intParent, Query1.strCategory, suppliers. *  FROM (

SELECT category.strCategory, category.intId, category.intParent FROM
category WHERE ( ( ( category.intParent ) =51 ) ) ) AS Query1 INNER
JOIN suppliers ON Query1.intId = suppliers.intCat ) AS Query2 INNER
JOIN tblcitylist ON Query2.intCity = tblcitylist.city_id ) AS Query3
INNER JOIN category ON Query3.intParent = category.intId

我希望从父类别(51)的所有子类别中获取供应商(包含其类别和父类别名称(strCategory,strParent)和城市名称(city_name为strCity))。

提前致谢。

1 个答案:

答案 0 :(得分:1)

这将通过传递父ID

返回父级,其类别和子类别
function get_parent_child_subchild($category_id,$all_cate=array())
{
    if(!is_array($category_id))
    {           
        $this->db->where('parent_id', $category_id);
        $all_cate[]=$category_id;
    }
    else
    {           
        $this->db->where_in('parent_id', $category_id);
    }        
    $get_categories= $this->db->get('newcategory'); 
    if($get_categories->num_rows()>0)
    {
        $categories_vales=$get_categories->result(); 
        $new_subcat = array();
        foreach($categories_vales as $cate_val)
        {
            $category_id=$cate_val->category_id;
            array_push($new_subcat,$category_id);
        }
        $all_cate = array_merge($all_cate,$new_subcat);         
        if(count($new_subcat)>0)
        {
            $this->get_parent_child_subchild($new_subcat,$all_cate);
        }   
    }   

    return $all_cate;
}