PHP Calculate Number of downline in binary tree

前端 未结 1 1364
伪装坚强ぢ
伪装坚强ぢ 2021-02-11 02:06

For calculating numbers of downline in binary tree, I am trying the following script by making 2 databases for registration and members downline structure. It works actually.

1条回答
  •  伪装坚强ぢ
    2021-02-11 02:19

    This is the answer after I try many ways. Solving my previous problem. Just using a single member table above.

    To show number of downlines, left and right. I insert this script in the HTML member tree page for every user in the tree A, down to B/C, down to D/E/F/G ):

    downline_number($member,'_left'); ?>
    downline_number($member,'_right'); ?>
    

    Add this function in User Class ;

    function downline_number($member,$position) {
    
    $query  = $this->db->prepare("SELECT * FROM `member` WHERE `upline`='$member' AND `position`='$position'");
            $query->bindValue(1, $member);
            $query->bindValue(2, $position);
    
    try{
            $query->execute();
            $rows = $query->fetch();
    
            if($this->count_downline($member,$position) >0 ){
            $total=$this->total_members_down($rows['username']);
            }else{
            $total=0;
            }
    
            return $total;      
    
            }catch(PDOException $e){
                die($e->getMessage());
            }   
    
        }   
    
    function count_downline($member,$position) {
    
    $query  = $this->db->prepare("SELECT * FROM `member` WHERE `upline`=? AND `position`=? ");
            $query->bindValue(1, $member);
            $query->bindValue(2, $position);
        try{
            $query->execute();
            return $rows = $query->rowCount();
    
            }catch(PDOException $e){
                die($e->getMessage());
            }   
        }   
    
    function total_members_down($upline,$reset=0) {
    global $num;
    if ($reset==0) { $num=1; }
    
    $query  = $this->db->prepare("SELECT * FROM `member` where `upline`='$upline' order by id asc");
            $query->bindValue(1, $upline);
    try{
    
    $query->execute();
    
    if ($upline !='') {
    
                if ($this->total_down($upline) > 0 ) {
                        while ($rows = $query->fetch() ) {
                        $num++;
                        $this->total_members_down($rows['username'],$num);
                        } 
                        return $num;
                } else { 
                return $num;
                }
    } else { $num=0; return $num;  }            
    
         }catch(PDOException $e){
                die($e->getMessage());
            }   
    }   
    
    function total_down($upline) {
    
    $query  = $this->db->prepare("SELECT * FROM `member` where `upline`='$upline' order by id asc ");
            $query->bindValue(1, $upline);
    
        try{
            $query->execute();
            return $rows = $query->rowCount();
    
            }catch(PDOException $e){
                die($e->getMessage());
            }   
        }   
    

    and it works showing binary member tree structure. Showing the memberID is not attached here, caused it simple way. Just left & right downlines number.

    Hope this post will help others who need it. Any suggestion for better ways?

    0 讨论(0)
提交回复
热议问题