0

I want list all the users registered directly and indirectly under a particular sponsor id

user tree

In my Database table, I have recorded the user like this - Database

--------+---------+-------+
userid  | under_id| side  |
--------+---------+--------
 1      |         |       | 
--------+---------+-------+ 
 2      | 1       | left  |
--------+---------+-------+
 3      | 1       | right |
--------+---------+-------+
 4      | 2       | left  |
--------+---------+-------+
 5      | 2       | right |
--------+---------+-------+
 6      | 3       | left  |
--------+---------+-------+
 7      | 3       | right |
--------+---------+-------+
.... so on.

Now assume a userid-2 login to his account and want to view his downline. So I want to display his downline in a table, similar to this -

  • Downline of Userid 2
--------+---------+-------+
userid  | under id| side  |
--------+---------+--------
 4      | 2       | left  |
--------+---------+-------+
 5      | 2       | right |
--------+---------+-------+
 8      | 4       | left  |
--------+---------+-------+
 9      | 4       | right |
--------+---------+-------+
 10     | 5       | left  |
--------+---------+-------+
 11     | 5       | right |
--------+---------+-------+
and so on...
$loop = 0;
$i = 1;
while($loop < $i){
    $query = "SELECT userid,under_id,side FROM usertable WHERE under_id=2";
    $result = $con->query($query);
    if($result->num_rows > 0){
        while($row = $result->fetch_assoc()){
            $data[] = array('userid'=>$row['userid'],'under id'=>$row['under_id'],'side'=>$row['side']);

            $que = "SELECT userid,first_name,side FROM mlm_user WHERE under_userid=$userid";
            $res = $con->query($que);
            if($res->num_rows > 0){
                while($rew = $res->fetch_assoc()){
                    $data[] = array('ID'=>$rew['userid'],'Name'=>$rew['first_name'],'Side'=>$rew['side']);
                    $userid = $rew['userid'];
                }
            }
        }

    }else{
        $i = 0;
    }
}

The above while loop is skipping few users, i.e not all users are getting selected.

I am not so expert. I know why it is not working as expected and how it can be fix (all in my imagination) but not able to code it :-(


Bips
  • 113
  • 2
  • 16

0 Answers0