Group logic and its functions to find all childs and all parents

Step 1.    Make a table like group_listing

id       group_name    group_description  parent_group_id    level

1          Vivek                  This is vivek group                  0                                          1

2       vivek child           This is vivek child                  1                                            2

3    vivek child 1           This is vivek child 1               2                                           3

4  vivek child child        most child                             3                                            4

 

Step 2 . Select all the parents group with name of    vivek child child group 

$select = mysql_query(“select * from $tablename) ;
$result_pag_data = mysql_fetch_assoc($select);
$arr=array();

foreach($result_pag_data as $Val)
{
$result=array();
$parent_id = $Val[‘parent_group_id’];
if($Val[‘level’]>1)
{
for ($i=$Val[‘level’];$i>1;$i–)
{
$parent_data= $this->Group->query(“SELECT name ,parent_group_id FROM $tablename WHERE id= “.$parent_id );
$parent_id =$parent_data[0][‘parent_group_id’];
$result[]=$parent_data[0][‘name’];
}
}

$reverse_name = implode(” -> “,array_reverse($result));

$demo= array(‘parent_names’=>$reverse_name);
$arr[] =array(‘lms_group_master’=>array_merge($demo,$Val[$tablename]));

}

//  $arr prints child group with all parents

 

Step 3.  Select all Child groups of parent group in ul li format

//Call this function set argument 0 it will return All children  of parent group as array

$data=  Chart_group(0);

 

function Chart_group($parent) {
$query = “SELECT * FROM “.$tablename.”  WHERE parent_group_id =”. $parent  ;
$result = $this->query($query);
$i = 0;
$children = array();
foreach($result as $data)
{
$children[$i][‘name’]=    $data[‘lms_group_master’][‘name’];
$children[$i][‘children’] = $this->Chart_group($data[‘lms_group_master’][‘id’],$luid);
$i++;
}
return $children;

}

// Call this function to View in ul li format , pass   $data array into it

$ul_li_format=     printList($array = null)

echo $ul_li_format;

function printList($array = null) {

if (count($array)) {
$this->html .= “<ul>”;
foreach ($array as $item) {
$this->html .= “<li>”;
$this->html .= $item[‘name’];
if (count($item[‘children’])) {
$this->printList($item[‘children’]);
}
$this->html .= “</li>”;
}

$this->html .=”</ul>”;
}
return $this->html;
}