I've got various solution from SO to create my first MLM project, but now I got stuck in total down-line count (via recursive function), as I've no prior Idea on this, please anyone help me.
My Database Table structure is as below (table name member):
`member_id | member_name | node_left | node_right
where member's relation is as:
member_id (Id 101)
/\
/ \
node_left(Id 102) node_right(Id 103)
/\ /\
/ \ blank / \blank
(again) blank node_right (104)
...... and so on. The above is just an example
`
Now I need to count total downline of any member. eg: suppose of above example, I want to know total downline of member_id 101 How to create the recursive function to do this which ends in finite loop ?
please give me开发者_如何学C any Idea..
I'm not sure if you've tried to implement the nested set model here, but the implementation doesn't look right..
With a nested set, the left/right values representing your tree structure would look like this:
member 101 (root): left=1, right=8
member 102: left=2, right=5
member 103: left=3, right=4
member 104: left=6, right=7
Then, counting the childs of member #101 would be as simple as:
SELECT COUNT(*) FROM member WHERE node_left > 1 AND node_right < 8
you can use/create your own customize function from the below code, Just check it out and try to implement for your case.
function Recursive_getsubcategory($parent_id,$cat_group,$intLevel)
{
global $intLevel;
$sql = "select *,".$intLevel." as level from tbl_name where parent_id = '".$parent_id."' ";
$result = mysql_query($sql);
$cat_name = $result->fetch_array_multiple();
if(count($cat_name) > 0)
{
for($k=0;$k<count($cat_name);$k++)
{
$cat_group[] = array('id'=>$cat_name[$k]['sub_id'],
'parent_id'=>$cat_name[$k]['parent_id'],
'level' => $cat_name[$k]['level']
);
$parent_id[] = $cat_name[$k]['parent_id'];
//Function For Recursive Get Sub Category...
Recursive_getsubcategory($cat_name[$k]['ebay_sub_id'],$cat_group,$intLevel++);
}
}
// count of total downline nodes
return count($cat_group);
}
This code may helpful for your task.
精彩评论