Get total left or right child in mlm binary plan

Hello friends. If you are planning to work on an mlm ( Multilevel Marketing ) website. And you need to find out total no of children in left or right leg.

This function will surely help you. It counts total left or right child of a member. You only need to pass the member_id and leg_position to this function. and it will return total no of children in your binary tree.

Please take care of your table structure. At a minimum it should be member_id(primary_key), parent_id and leg_position where leg_position contains the position of the child member i.e. left or right.

Note : You need to reset the $total variable to 0 before every function call otherwise it will return garbage value.
  function getTotalLeg($memid,$leg){
  $sql="select member_id from members where 
        parent_id='".$memid."' and leg_position='".$leg."'";
    
  $res=mysql_query($sql);
  
  global $total;
    
  $total=$total+mysql_num_rows($res);
  $row=mysql_fetch_array($res);
 
     if($row['member_id']!=''){
       getTotalLeg ($row['member_id'],'l');
       getTotalLeg ($row['member_id'],'r');
      }

    return $total;
    }


Function Call

$total=0;
$left=getTotalLeg(''mlm10002","l");
$total=0;
$right=getTotalLeg("mlm10002","r");

In the above function call "l" denotes left leg and "r" denotes right leg position stored in the table.
Get total left or right child in mlm binary plan Get total left or right child in mlm binary plan Reviewed by JS Pixels on July 26, 2011 Rating: 5

52 comments:

  1. Why this always give 0?
    [code]
    $total =0;

    $memid = $_POST['username'];
    $leg = $_POST['leg'];

    mysql_connect ("host", "usr", "paswd") or die ('Error: ' .mysql_error());
    mysql_select_db ("dbname");
    function getTotalLeg($memid,$leg){
    $sql="select $leg from `users` where `username`='$memid' ";

    $res=mysql_query($sql);
    $row=mysql_fetch_array($res);

    global $total;

    $total = $total+mysql_num_rows($res);
    if($row['$leg']!=''){
    getTotalLeg ($row['$leg'],'lname');
    getTotalLeg ($row['$lrg'],'rname');
    }
    return $total;
    }
    echo $total;
    [/code]

    ReplyDelete
  2. I think there is a problem with your table structure.


    My table struture is

    member_id - Unique id for each member.
    parent_id - The parent of the member.
    leg_position - It is the position of the member from its parent could be either right or left.

    From your code I assume some fields as

    username - May be unique username or userid of a member
    lname and rname being position of the member from its parent.
    parent_id is missing....

    Modify your table and try again. Thanks !!

    ReplyDelete
  3. Hi,
    Altaf i have read your code but unable to count left & right.I am also php developer.
    can you help me some tips.

    ReplyDelete
  4. Hi,
    Altaf i've used your code for my script it works perfectly.
    Thanks for your help.

    Arun.

    ReplyDelete
  5. Dear Altaf Bhai,
    Take my salam. I am new in php project. i basic vb programmer.i have mlm project in my hand.but i face a problem in counting left and right child in tree. My database same as your instructin.please send function file and function call page.

    ReplyDelete
  6. Altaf hussain

    hi , i am working in mlm project i used your script but calculation comes wrong
    according to my structure it should be 8:9
    but its show 8:17



    $memid=$_SESSION['btregister'];


    $total =0;
    function getTotalLeg($memid,$leg)
    {

    $sql="SELECT elmember_id FROM member_login where elsponser_id='".$memid."' AND elposition='".$leg."'";
    $res=mysql_query($sql);
    $row=mysql_fetch_array($res);
    global $total;

    $total=$total+mysql_num_rows($res);
    if($row['elmember_id']!=''){
    getTotalLeg ($row['elmember_id'],'left');
    getTotalLeg ($row['elmember_id'],'right');
    }
    return $total;
    }

    ReplyDelete
  7. ok i have solved this problem but through your code its give wrong calculation

    ReplyDelete
  8. @deepak, well

    It gives you 8:17 in place of 8:9 because of the global variable. You have to reset the global variable before calling function again like...


    //Call function
    $left=getTotalLeg(''mlm10002","l");

    //reset the $total value
    $total=0;

    //Call the function again
    $right=getTotalLeg("mlm10002","r");

    ReplyDelete
  9. ok its working, thanxs for your code

    ReplyDelete
  10. How ti give function call to this function .bucause it showing answer 0.

    Function Call
    $left=getTotalLeg(''mlm10002","l");
    $total=0;
    $right=getTotalLeg("mlm10002","r");

    ReplyDelete
  11. Hay Altaf ,what is
    Function Call
    $left=getTotalLeg(''mlm10002","l");
    $total=0;
    $right=getTotalLeg("mlm10002","r");

    mlm10002 in this function.


    ////////////////////////////////
    my code is


    $total =0;
    function getTotalLeg($iid,$leg){
    $sql="select id from members where sponsor=2 and side='".$leg."'";

    $res=mysql_query($sql);

    global $total;


    $total=$total+mysql_num_rows($res);
    $row=mysql_fetch_array($res);

    $left=getTotalLeg("id","l");
    $total=0;
    $right=getTotalLeg("id","r");

    if($row['id']!=''){
    getTotalLeg ($row['id'],'l');
    getTotalLeg ($row['id'],'r');
    }
    return $total;
    }
    echo $total;

    then where i put function call code here..please help me..

    ReplyDelete
  12. @swapnil.

    mlm10002 is member id and l is the left leg position.

    When you count the total left child of a member id 2, you have to call it as follows

    $left=getTotalLeg("2","l");

    and in case o f right call it as

    $left=getTotalLeg("2","r");

    Please review your code and try again

    ReplyDelete
  13. Hy altaf my left count showing less than 2 i.e actually it is 9 but it show 7 and right also 9 but it show 11..what is problem.i show you my code..please help mi,barbecue my project stop due to this node count.

    $total =0;
    function getTotalLeg($iid,$leg)
    {

    $sql="SELECT * FROM tbl_member where sponsor='".$iid."' AND side='".$leg."'";
    $res=mysql_query($sql);
    $row=mysql_fetch_array($res);
    global $total;

    $total=$total+mysql_num_rows($res);
    if($row['id']!=''){
    getTotalLeg ($row['id'],'l');
    getTotalLeg ($row['id'],'r');
    }
    return $total;

    }
    $left=getTotalLeg($iid,"l");
    echo $total;
    $total=0;
    $right=getTotalLeg($iid,"r");
    echo $total;

    ReplyDelete
  14. Hi altaf thanks a lot, your code working properly... thanks for the help...Advance Eid Mubarak....

    ReplyDelete
    Replies
    1. Hello Swapnil Patil can u help me i am working on mlm can u give me database or code in php

      Delete
  15. Hi Altaf,

    Thanks for your code, actually i m a asp.net developer, and currently i m working on a mlm website, and your code helped me to solve my problem.

    Thanks once again
    (Pardeep)
    pardeep3@gmail.com

    ReplyDelete
  16. hi altaf

    can you please help me show the binary tree for mlm website

    ReplyDelete
  17. hi i am getting some problem that ,

    my database :

    user_id | uplink | position | pack |

    k90 k89 Left Product
    k91 k90 Left Register

    so, I want to count how much product user left or right . Not show Register user only show product user . plz help me ?

    ReplyDelete
  18. @ Techsentence

    Before incrementing the counter check whether it is product user or register user. Assuming your table name as members the function would be

    function getTotalLeg($memid,$leg){
    $sql="select user_id,pack from members where
    uplink='".$memid."' and position='".$leg."'";

    $res=mysql_query($sql);

    global $total;

    $row=mysql_fetch_array($res);
    if($row['pack']=='Product'){
    $total=$total+mysql_num_rows($res);
    }

    if($row['user_id']!=''){
    getTotalLeg ($row['user_id'],'Left');
    getTotalLeg ($row['user_id'],'Right');
    }

    return $total;
    }

    ReplyDelete
  19. Dear Brother,

    my database have two type member (register and product) in same row , main problem is that lot of register member in left site also few product member left site , but it count by serial , when found register member left site rest of the below product member didn't count . So please help me ??

    here is my code :

    // for left product member count

    $ttg=0;
    $tt2g=0;
    $tt2rg=0;
    function total_downlinkpg($id)
    {
    //echo $id;
    $exe3g=mysql_query("SELECT user_id,pack FROM member where uplink='".$id."' and position='Left'");
    $ttg=$ttg+mysql_num_rows($exe3g);

    while($re3g=mysql_fetch_array($exe3g))

    if($row['pack']=='Product')
    {
    $ttg=$ttg+total_downlinkp2g($re3g['user_id']);
    }

    return $ttg;

    }
    function total_downlinkp2g($id)
    {
    //echo $id;
    $exe4g=mysql_query("SELECT user_id,pack FROM member where uplink='".$id."' and position='Left'");
    $cc4g=mysql_num_rows($exe4g);

    while($re4g=mysql_fetch_array($exe4g))

    if($row['pack']=='Product')
    {
    $tt2g=$tt2g+total_downlinkp2g($re4g['user_id']);
    }

    //echo $id;
    $exerg=mysql_query("SELECT user_id,pack FROM member where uplink='".$id."' and position='Right' ");
    $ccrg=mysql_num_rows($exerg);
    while($rerg=mysql_fetch_array($exerg))
    if($row['pack']=='Product'){
    $tt2rg=$tt2rg+total_downlinkp2g($rerg['user_id']);

    }


    return $cc4g+$tt2g+$ccrg+$tt2rg;

    }

    ReplyDelete
  20. Assalam o Alaikum Bhai

    Mujhe ek validation lagana hai JS ya jquery jo bhi better ho.

    Ek form hain mujhe onsubmit pe validation lagana hain ki agar user textarea mein message type karta hai Toh us msg mein agar woh word aata hain jo maine array mein sotre kiya hai to alert aana chahiye ki 'you r use promotional word which r not allow' plz reply & thanks in advance

    function validate(){


    var test_words = new Array("OFFER", "EXHIBITION", "PRICE", "EXCLUSIVE", "CHEAP", "AFFORDABLE", "TOUR", "TRAVEL", "SERVICES", "BOTTOM", "SPECIAL", "DISCOUNT","GUARANTEE", "WARRANTY", "FANCY", "LIMITED", "PERIOD", "SALE", "PURCHASE", "WWW", "SHOP", "REASONABLE", "BUY", "GET", "MORE", "LESS", "STOCK", "WHOLESALE", "WHOLE SALE", "EXCHANGE", "DISTRIBUTOR");


    var user_words = document.getElementById('message').split(/\W+/);

    for( var i in test_words)
    {
    if( user_words.indexOf( test_words[i] ) != -1 )
    {
    alert( 'The textarea has test_words!');
    }
    alert('suc');
    }
    }


    ReplyDelete
  21. Walaikum Assalam.

    Try this

    function validate(){

    var test_words = new Array("OFFER", "EXHIBITION", "PRICE", "EXCLUSIVE", "CHEAP", "AFFORDABLE", "TOUR", "TRAVEL", "SERVICES", "BOTTOM", "SPECIAL", "DISCOUNT","GUARANTEE", "WARRANTY", "FANCY", "LIMITED", "PERIOD", "SALE", "PURCHASE", "WWW", "SHOP", "REASONABLE", "BUY", "GET", "MORE", "LESS", "STOCK", "WHOLESALE", "WHOLE SALE", "EXCHANGE", "DISTRIBUTOR");

    var flag=0;
    var user_words = document.getElementById('message').value;

    var u_words=user_words.split(' ');

    for(i=0; i<test_words.length; i++){
    for(j=0; j<u_words.length; j++){
    if(u_words[j].toLowerCase()==test_words[i].toLowerCase()){
    flag=1;
    break;
    }
    }
    }

    if(flag){
    alert('found');
    }else{
    alert('success');
    }
    }

    ReplyDelete
  22. jazak allah bhai actually mein fresher hoon abhi php mein aur mein do din se pareshaan ho raha tha.
    Allah aapko Jazaye khair de aur taraqqi ata farmaye. Aur apne deen k liye bhi qubool farmaye.

    ReplyDelete
  23. plz give me idea which things are need to develop MLM website DATABASE

    It is in binary form

    Thanks in advance

    ReplyDelete
  24. Bhai mujhe pehla project mila hai MLM per mujhe smajh mein nhi aa raha hai how to start and how to create its database. Plz Help me

    ReplyDelete
  25. Assalam o Alaikum
    Bhai Mujhe advance search banana hai jisme table ki field hai :

    `job_details` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `title` varchar(255) NOT NULL,
    `job_type` varchar(255) DEFAULT NULL,
    `company_id` int(11) NOT NULL,
    `eligibility` varchar(255) DEFAULT NULL,
    `designation` varchar(255) DEFAULT NULL,
    `category_id` int(11) NOT NULL,
    `location` varchar(255) DEFAULT NULL,
    `last_date` varchar(255) DEFAULT NULL,
    `hiring_process` text,
    `edu_qualification` text,
    `experience` text,
    `job_desc` text,
    `application_link` text,
    `DateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `DateUpdated` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)

    Aur text field hai title,category(int),qualification,location,designation,experince

    Maine Jo query lagaai hai hai woh hai

    "SELECT * FROM `job_details` WHERE `title` LIKE '%$title%' OR `job_type` LIKE '%$keyword% ' OR `eligibility` LIKE '%$qua% ' OR `designation` LIKE '%$designation% ' OR `category_id` LIKE '%$category%' OR `job_desc` LIKE ' %$keyword%' OR `experience` LIKE '%$exp%'"

    ReplyDelete
  26. Sir,
    I want to make a MLM website in php and for that I need your help that how to create database and generate binary system. i tried but failed to do so.
    please can you mail me the sample to my email id.
    thanks

    ReplyDelete
  27. Sir,
    I want to make a MLM website in php and for that I need your help that how to create database and generate binary system. i tried but failed to do so.
    please can you mail me the sample to my email id.
    thanks

    ReplyDelete
  28. there is any solution to get last pair member id of downline

    ReplyDelete
  29. there is any solution to get last pair member id of downline

    ReplyDelete
  30. hi i m manoj
    my code is same as your
    but its going only left side

    if($row['member_id']!=''){
    getTotalLeg ($row['member_id'],'l');
    then by left get the sponser then execute that right function
    getTotalLeg ($row['member_id'],'r');

    but not directly member id execute both function

    }

    ReplyDelete
  31. high altaf can you help, i use your code and it works good, i used it in my basic membership where they only earn low commission when left and right match even if they have a gold member or basic member in their tree,

    while YOU as gold member will count first the gold member in your tree and when left right match it pay high commission then it will count again the left and right of basic member under your tree and when match found you earn again low commission,

    ReplyDelete
  32. I solve it now i just misplace some of my code,by the way thank you for your code it help me a lot, ,

    ReplyDelete
  33. any one give me database table structure or link of working project to download

    ReplyDelete
  34. Dear Altaf Bhai can help me to display a binary tree according to your table structure. I will be thankful if you could give me the snapshot of database also

    ReplyDelete
  35. hello and thank you for posting this article,it helps me alot in my project,your code only gives left ond right pairs,please help me to calculate total pair of a binary plan where 2:1 is first pair and others are 1:1,thanks in advance

    ReplyDelete
  36. Altaf vai , really you are great . your code is awesome . thanks a lot.

    ReplyDelete
  37. Thanks
    Altaf Bhai ..... your code is right n woking fine....

    ReplyDelete
    Replies
    1. Welcome Tembhre,
      Nice to know it helped !!

      Delete
    2. Hello sir,, please help me
      i want MLM dynamic binary tree code in PHP and MySQL in my project
      i want code demo
      Please anyone help for me?

      Delete
  38. Hi altaf hussain!
    can you show me the database look with few values to use this function because i have tried but its saying the execution time execeeds 30 seconds but i kept only 5 to 10 entries in members table. Kindly reply me.

    ReplyDelete
  39. i am using php and mysql mlm table
    i want to generated left right sum of amount


    this is my table
    table name is investment
    `Invested_user` means user id means member name
    `uplink` id means who is upline who is sponsor
    `xleft` means left side
    `xright` means right side
    `invest_amount` i want to sum invest_amount left and right side

    here is some example data
    =============================
    Monir- Jamal - L 4000
    Jahir -Jamal - R 2000
    Jasim -Monir - R 1000

    ReplyDelete
  40. hi want to know how to count each level member in downline
    as like if user id is 2 has 10 member in downline
    in level1 2 member
    in level 2 4 member
    in level 3 1 member
    in level4 3 member
    please help me

    ReplyDelete
  41. hello ...
    can you help me get the the total number of downlines of a certain user ... this is my current table for genealogy => fields: iGenealogyId (ai), iParentId, iLeftId, iRightId .... and this is my query ...
    $sql = "SELECT DISTINCT(iLeftId) FROM genealogy WHERE iParentId = '".$_SESSION["sess_iMemberId"]."' AND iLeftId != 0";
    $db_left = $obj->MySQLSelect($sql);

    $sql = "SELECT DISTINCT(iRightId) FROM genealogy WHERE iParentId = '".$_SESSION["sess_iMemberId"]."' AND iRightId != 0";
    $db_right = $obj->MySQLSelect($sql);

    $left = array_map("current",$db_left);
    $right = array_map("current",$db_right);

    $doqnlines = array_merge($left, $right);
    $doqnlines = array_unique($doqnlines);

    but it shows only the Direct members of a certain user ...
    can you help me get the total downlines/child ..

    Thanks

    ReplyDelete
  42. hi Altaf can you show me how to calculate no of members in each level for both the sides

    ReplyDelete
  43. i want MLM dynamic binary tree code in PHP and MySQL in my project
    i want code demo
    Please anyone help for me?

    ReplyDelete
  44. Hello Altaf , can u plz help me in inserting data and counting of left, right, total children in mlm with database structure

    ReplyDelete
  45. help me for the commission calculation, like first first get 500 commission once he had the right and left child how to add it for database

    ReplyDelete
  46. Hello Altaf,

    I code see 500 Internal Error but why?

    ReplyDelete

  47. hi, I want to calculate binary income...can someone help me ...my site is in php mysql.

    ReplyDelete
  48. You save my all hard work now am able to complete my project Thanks

    ReplyDelete

Altaf Web. Powered by Blogger.