我已经使用 codeigniter 作为前端 mysql 作为后端创建了 MLM 项目。
这里我对获取 child 数量有疑问。
这是我的表格流程:
这是我的树流:
This is my query How to get Multi-level marketing (tree) child count.
例如: 主用户有 14 个子用户。
我试过这段代码:
查看:
$product2_users = $this->login->getProduct2Users($top_id);
型号:
public function getProduct2Users($top_id)
{
$count = 0;
$this->db->select('id');
$this->db->from('member');
$this->db->where('sponsor', $top_id);
$first_child = $this->db->get();
foreach($first_child->result_array() as $f_child)
{
$f_child_id[] = $f_child['id'];
if(isset($f_child_id))
{
$this->db->select('id');
$this->db->from('member');
$this->db->where('sponsor', $f_child_id);
$second_child = $this->db->get();
}
}
echo'<pre>';print_r($second_child->result_array());exit;
return $first_child->result_array();
}
最佳答案
如果你真的想要一个解决方案,你必须以递归的方式来做
像下面这样的东西应该可以工作
public function getCountChilds($id)
{
$count = 0;
$query = $this->db
->select('id')
->from('member')
->where('sponsor', $id)
->get();
if ($query->num_rows() > 0)
{
foreach($query->result() AS $objChild)
{
$count += $this->getCountChilds($objChild->id);
++ $count;
}
}
return $count;
}
关于mysql - 如何获得多层次营销(树)子计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52402721/