按给定的类别ID显示类别树,子类别

时间:2013-10-18 11:18:05

标签: php codeigniter recursion treeview

虽然我正在使用codeigniter,但是如果有人可以帮我处理简单的递归函数并不重要。 我有一个名为“tbl_member”的表,其中有3个字段member_id,parent_id,name 我想要tree structure特定member id

例如,如果我发送member_id(10),则会显示children(sub_member)所有 member

3 个答案:

答案 0 :(得分:1)

有帮助吗?

 function get_tree($member_id){
  $result=$this->db->get_where('table',array('member_id'=>$member_id));
  $data=$result->row_array();
    $n=$this->get_child($data['member_id']);
      foreach($n as $l){
        $data[]=$l;
       }
    print_r($data);
}


function get_child($parent_id){
$result=$this->db->get_where('table',array('parent_id'=>$parent_id));
$row = $result->result_array();
foreach ($row as $key => $m) {
  if (is_array($m) && count($m) != NULL) {
      $me=$this->get_child($m['member_id']);
      foreach($me as $res){
            $row[$key][]=$res;
       }
    }
 }
  return $row;
}

答案 1 :(得分:0)

function getMemberTree($id) {
$q =  $this->db->get_where('tbl_member', array('parent_id' => $id));
if($q->num_rows() > 0) {
    foreach($q->result() as $row) {
        $first_level[] = $row;
    }
    foreach($first_level as $r) {
        $member_id = $r->member_id;
        $m = $this->db->get_where('tbl_member', array('parent_id' => $member_id));
        if($m->num_rows() > 0) {
            foreach($m->result() as $mrow) {
                $second_level[] = $mrow;
            }
        }
    }
    $data[] = $first_level;
    $data[] = $second_level;

    return $data; 

}

答案 2 :(得分:0)

这些解决方案需要许多数据库命中。请考虑使用层次结构表结构:

http://codebyjeff.com/blog/2012/10/nested-data-with-mahana-hierarchy-library