php - create parent and its child tree in codeigniter -
i working in codeigniter.i want display parent agent , child agent under parent agent. parent agent data display this
array ( [0] => stdclass object ( [id] => 1 [introducer_code] => 0 [designation] => 2 [cader] => [code] => [name] => vinod [area] => [d_w_s] => rajendra [gender] => male [dob] => 2014-12-01 [age] => 25 [mobile_no] => 123456789 [village] => vadodara road [city] => vadodara [district] => vadodara [state] => 1 [pincode] => 391212 [pan] => bcd1234587 [nominee] => rajendra [n_relation] => father [n_age] => 35 [d_o_j] => 2014-12-22 [amount] => 100 [bank_acc] => 0123467 [bank_add] => vadodara [branch_id] => 102 [uname] => [pass] => [enc_pass] => d41d8cd98f00b204e9800998ecf8427e [agent_id] => [profile_pic] => ) )
here introducer_code parent agent code.now want display child agent introduce code id 1.
my code this.
public function get_agent_tree_commision() { $query = $this->db->query("select * agent id = '1'"); $result = $query->result(); echo "<pre>"; print_r($result); $roles = array(); foreach($result $key=>$value) { if($result[$key]->introducer_code != 0) { $role = array(); $role['id'] = $result[$key]->id; $role['name'] = $result[$key]->name; $children = $this->build_child($result, $result[$key]->id); //print_r($children); if( !empty($children) ) { $role['children'] = $children; } $roles[] = $role; } } return $roles; //$this->load->view("cashier/get_agent_tree_commision"); } public function build_child($result, $parent) { $roles = array(); foreach($result $key => $val) { if($result[$key]->introducer_code == $parent) { $role = array(); $role['role_id'] = $result[$key]->id; $role['role_name'] = $result[$key]->name; $children = $this->build_child($result, $result[$key]->id); if( !empty($children) ) { $role['children'] = $children; } $roles[] = $role; return $roles; } } }
i have 4 child agent introducer code 1. , result this.
array ( [0] => stdclass object ( [id] => 2 [introducer_code] => 1 [designation] => 1 [cader] => [code] => [name] => nisarg bhavsar [area] => [d_w_s] => bhavsar [gender] => male [dob] => 2014-12-01 [age] => 19 [mobile_no] => 123456789 [village] => vadodara road [city] => vadodara [district] => vadodara [state] => 1 [pincode] => 391212 [pan] => bcd1234587 [nominee] => bhavsar [n_relation] => father [n_age] => 35 [d_o_j] => 2014-12-22 [amount] => 100 [bank_acc] => 0123467 [bank_add] => vadodara [branch_id] => 11 [uname] => [pass] => [enc_pass] => d41d8cd98f00b204e9800998ecf8427e [agent_id] => [profile_pic] => ) [1] => stdclass object ( [id] => 3 [introducer_code] => 1 [designation] => 1 [cader] => [code] => [name] => test1 [area] => [d_w_s] => test [gender] => male [dob] => 2004-12-01 [age] => 25 [mobile_no] => 123456789 [village] => vadodara road [city] => vadodara [district] => vadodara [state] => 1 [pincode] => 391212 [pan] => bcd1234587 [nominee] => test [n_relation] => father [n_age] => 40 [d_o_j] => 2014-12-26 [amount] => 100 [bank_acc] => 0123467 [bank_add] => vadodara [branch_id] => 11 [uname] => [pass] => [enc_pass] => d41d8cd98f00b204e9800998ecf8427e [agent_id] => [profile_pic] => ) [2] => stdclass object ( [id] => 4 [introducer_code] => 1 [designation] => 1 [cader] => [code] => [name] => test [area] => [d_w_s] => modi [gender] => male [dob] => 1985-04-01 [age] => 21 [mobile_no] => 2147483647 [village] => near petrol pump [city] => vadodara [district] => vadodara [state] => 1 [pincode] => 391300 [pan] => gdt126985 [nominee] => tester [n_relation] => father [n_age] => 45 [d_o_j] => 2015-04-14 [amount] => 100 [bank_acc] => [bank_add] => [branch_id] => 112 [uname] => test@test.com [pass] => 1234 [enc_pass] => 81dc9bdb52d04dc20036dbd8313ed055 [agent_id] => [profile_pic] => ) [3] => stdclass object ( [id] => 5 [introducer_code] => 1 [designation] => 1 [cader] => [code] => [name] => bhumi [area] => [d_w_s] => bhumi [gender] => female [dob] => 2015-04-16 [age] => 5 [mobile_no] => 2147483647 [village] => valsad [city] => valsad [district] => valsad [state] => 1 [pincode] => 396001 [pan] => abfcjh9798h [nominee] => bhumi [n_relation] => other [n_age] => 05 [d_o_j] => 2015-04-15 [amount] => 0 [bank_acc] => [bank_add] => [branch_id] => 112 [uname] => [pass] => [enc_pass] => [agent_id] => [profile_pic] => ) )
when run code display nothing. now, code should have write display agent under agent?
i have changed little bit change in code , working. code :
public function get_agent_tree_commision() { $query = $this->db->query("select * agent id = '1'"); $result = $query->result(); $roles = array(); foreach($result $key=>$value) { $role = array(); $role['id'] = $result[$key]->id; $role['name'] = $result[$key]->name; $children = $this->build_child($result[$key]->id); if( !empty($children) ) { $role['children'] = $children; } $roles['role'] = $role; } $this->load->view("cashier/get_agent_tree_commision",$roles); } public function build_child($parent) { $query = $this->db->query("select * agent introducer_code = '$parent'"); $result = $query->result(); $roles = array(); foreach($result $key => $val) { if($result[$key]->introducer_code == $parent) { $role = array(); $role['id'] = $result[$key]->id; $role['name'] = $result[$key]->name; $children = $this->build_child($result[$key]->id); if( !empty($children) ) { $role['children'] = $children; } $roles[] = $role; } } return $roles; }
changes:
1)in get_agent_tree_cummision() function have changed line
$children = $this->build_child($result[$key]->id);
2)in build_child($parent) function have added query
$query = $this->db->query("select * agent introducer_code = '$parent'"); $result = $query->result();
3)and have return $roles out of foreach loop in build_child($parent) function.
finally, works perfect.
Comments
Post a Comment