0

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

例如,如果我发送一个member_id(10)它会显示所有 children(sub_member)这些member

4

3 回答 3

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;
}
于 2013-10-18T12:02:06.987 回答
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; 

}

于 2013-10-18T12:08:33.237 回答
0

这些解决方案需要很多很多的数据库命中。考虑一个层次表结构:

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

于 2013-10-18T12:17:20.007 回答