Php 在codeigniter中创建父树及其子树
我在codeigniter中工作。我想在父代理下显示父代理及其子代理。 我的父代理数据显示如下Php 在codeigniter中创建父树及其子树,php,codeigniter,Php,Codeigniter,我在codeigniter中工作。我想在父代理下显示父代理及其子代理。 我的父代理数据显示如下 Array ( [0] => stdClass Object ( [id] => 1 [Introducer_code] => 0 [Designation] => 2 [Cader] => [Code] => [Name] => Vinod
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] =>
)
)
这里的介绍人代码是父代理代码。现在我想显示其介绍人代码id为1的子代理
我的代码是这样的
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;
}
}
}
<?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();
}
}
当我运行此代码时,它不会显示任何内容。
现在,我应该编写什么代码来显示代理下的代理?我为Codeigniter转换了一个PHP类,该类处理父子关系
它位于我不知道您使用哪个CI版本,但如果是V3,您可以尝试以下操作: 在您的模型中:
<?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);
}
}
孩子们
我对我的代码做了一点小小的修改,现在它开始工作了。
我的代码是:
$children = $this->build_child($result[$key]->id);
变化:
1) 在get\u agent\u tree\u cummision()函数中,我更改了这一行
$query = $this->db->query("select * from agent where Introducer_code = '$parent'");
$result = $query->result();
2) 在build_child($parent)函数中,我添加了查询
3) 在build_child($parent)函数中,我从foreach循环中返回了$roles
最后,它工作得非常完美。我的codeigniter版本是2.2.0。那么我可以使用此代码吗?我快速查看了文档-看起来应该可以试试-如果您在此处发布错误时遇到任何问题,它可以工作,但会给出错误的结果。我们必须使用介绍人代码作为代理id并显示结果。我在loadChildAgents中将查询更改为(“介绍人代码”,$objAgent->id)。但它不显示id为2的子级。在函数loadAgent writeecho$this->db->last_query()中代码>在$query->…->get()行下代码>和get()调用下面的loadChildAgents函数中的相同内容,并发布输出pleaseThanks,以便给我回复。
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;
}
$children = $this->build_child($result[$key]->id);
$query = $this->db->query("select * from agent where Introducer_code = '$parent'");
$result = $query->result();