在codeigniter中创建父树及其子树

时间:2021-10-26 21:34:58

I am working in codeigniter.I want to display parent agent and its child agent under the parent agent. My parent agent data display like 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 is parent agent code.Now I want to display child agent whose introduce code is id as 1.

这里介绍的代码是父代理代码。现在我想要显示子代理,它的引入代码是id为1。

My code is like this.

我的代码是这样的。

public function get_agent_tree_commision()
{   

    $query = $this->db->query("select * from agent where id = '1'");

    $result = $query->result();
    echo "<pre>";
    print_r($result);

    $roles = array();

    foreach($result as $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 as $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 four child agent whose introducer code is 1. And its result like this.

我有四个子代理,它们的介绍人代码是1。结果是这样的。

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 I run this code it display nothing. Now, what code should I have to write to display agent under agent?

当我运行这段代码时,它什么也不显示。现在,我需要写什么代码来显示agent在agent下?

3 个解决方案

#1


2  

I have changed little bit change in my code and now it working. My code is :

我对代码做了一点修改,现在它可以工作了。我的代码是:

public function get_agent_tree_commision()
{   
    $query = $this->db->query("select * from agent where id = '1'");

    $result = $query->result();

    $roles = array();

    foreach($result as $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 * from agent where Introducer_code = '$parent'");

    $result = $query->result();

    $roles = array();       

    foreach($result as $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 I have changed this line

1)在get_agent_tree_cummision()函数中,我更改了这一行

$children = $this->build_child($result[$key]->id);

2)In build_child($parent) function I have added query

在build_child($parent)函数中,我添加了查询

$query = $this->db->query("select * from agent where Introducer_code = '$parent'");

$result = $query->result();

3)And I have return $roles out of the foreach loop in build_child($parent) function.

3)在build_child($parent)函数中,我从foreach循环中返回$roles。

Finally, it works perfect.

最后,它是完美的。

#2


0  

I converted a PHP class for Codeigniter that does parent child relationships.

我为Codeigniter转换了一个PHP类,该类处理父子关系。

It's located here.

它坐落在这里。

#3


0  

I've no idea which CI Version u use but in case of V3 u can try the following:

我不知道你用的是哪种CI版本,但是如果是V3,你可以试试下面的:

in your model:

在你的模型:

<?php
class Agent_Model extends CI_Model
{

    public function loadAgent($id)
    {
        $query = $this->db
            ->select("*")
            ->from("agent")
            ->where("id",$id)
            ->get();

        $objAgent = $query->row(0, "Agent_Object");
        $this->loadChildAgents($objAgent);

        return $objAgent;
    }

    private function loadChildAgents(Agent_Object $objAgent)
    {

        $query = $this->db
            ->select("*")
            ->from("agent")
            ->where("Introducer_code",$objAgent->Introducer_code)
            ->get();

        if ($query->num_rows() > 0)
        {
            foreach($query->result() AS $objChild)
            {
                $objAgent->addChild($objChild);
            }
        }
    }
}

class Agent_Object
{
    private $arrChilds = array();


    public function addChild($objChild)
    {
        $this->arrChilds[] = $objChild;
    }

    public function getChilds()
    {
        $obj = new ArrayObject($this->arrChilds);
        return $obj->getIterator();
    }

}

and in your controller

和在你的控制器

<?php

class Agent extends CI_Controller
{

    public function __construct()
    {
        $this->load->model("Agent_Model");
    }

    public function agentTree()
    {
        $objAgent = $this->Agent_Model->loadAgent(1);

        $arrViewData = array("objAgent" => $objAgent);

        $this->load->view("cashier/get_agent_tree_commision",$arrViewData);
    }
}

and in your view

在你看来

<?php
echo $objAgent->Name;
?>
Childs
<?php
foreach($objAgent->getChilds() AS $key => $objChild)
{
    print_r($objChild);
}

#1


2  

I have changed little bit change in my code and now it working. My code is :

我对代码做了一点修改,现在它可以工作了。我的代码是:

public function get_agent_tree_commision()
{   
    $query = $this->db->query("select * from agent where id = '1'");

    $result = $query->result();

    $roles = array();

    foreach($result as $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 * from agent where Introducer_code = '$parent'");

    $result = $query->result();

    $roles = array();       

    foreach($result as $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 I have changed this line

1)在get_agent_tree_cummision()函数中,我更改了这一行

$children = $this->build_child($result[$key]->id);

2)In build_child($parent) function I have added query

在build_child($parent)函数中,我添加了查询

$query = $this->db->query("select * from agent where Introducer_code = '$parent'");

$result = $query->result();

3)And I have return $roles out of the foreach loop in build_child($parent) function.

3)在build_child($parent)函数中,我从foreach循环中返回$roles。

Finally, it works perfect.

最后,它是完美的。

#2


0  

I converted a PHP class for Codeigniter that does parent child relationships.

我为Codeigniter转换了一个PHP类,该类处理父子关系。

It's located here.

它坐落在这里。

#3


0  

I've no idea which CI Version u use but in case of V3 u can try the following:

我不知道你用的是哪种CI版本,但是如果是V3,你可以试试下面的:

in your model:

在你的模型:

<?php
class Agent_Model extends CI_Model
{

    public function loadAgent($id)
    {
        $query = $this->db
            ->select("*")
            ->from("agent")
            ->where("id",$id)
            ->get();

        $objAgent = $query->row(0, "Agent_Object");
        $this->loadChildAgents($objAgent);

        return $objAgent;
    }

    private function loadChildAgents(Agent_Object $objAgent)
    {

        $query = $this->db
            ->select("*")
            ->from("agent")
            ->where("Introducer_code",$objAgent->Introducer_code)
            ->get();

        if ($query->num_rows() > 0)
        {
            foreach($query->result() AS $objChild)
            {
                $objAgent->addChild($objChild);
            }
        }
    }
}

class Agent_Object
{
    private $arrChilds = array();


    public function addChild($objChild)
    {
        $this->arrChilds[] = $objChild;
    }

    public function getChilds()
    {
        $obj = new ArrayObject($this->arrChilds);
        return $obj->getIterator();
    }

}

and in your controller

和在你的控制器

<?php

class Agent extends CI_Controller
{

    public function __construct()
    {
        $this->load->model("Agent_Model");
    }

    public function agentTree()
    {
        $objAgent = $this->Agent_Model->loadAgent(1);

        $arrViewData = array("objAgent" => $objAgent);

        $this->load->view("cashier/get_agent_tree_commision",$arrViewData);
    }
}

and in your view

在你看来

<?php
echo $objAgent->Name;
?>
Childs
<?php
foreach($objAgent->getChilds() AS $key => $objChild)
{
    print_r($objChild);
}