PHP Calculate the number of downstream elements in a binary tree

For calculating the bottom level numbers in a binary tree, I am trying to use the following script by creating two databases to register and a member structure. It actually works.

But the member database is growing very quickly. A called level n in the binary tree will generate n entries for one user registration. I am just wondering if the user is logged in at 1000 level, then it will create 1000 records when one user logs in.

Any other solution for this system?

Complete long script:

CREATE TABLE IF NOT EXISTS `member` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `username` varchar(55) CHARACTER SET utf8 NOT NULL,
  `upline` varchar(55) CHARACTER SET utf8 NOT NULL,
  `position` varchar(10) NOT NULL,
  `sponsor` varchar(55) CHARACTER SET utf8 NOT NULL,
  `_left` varchar(55) CHARACTER SET utf8 NOT NULL,
  `_right` varchar(55) CHARACTER SET utf8 NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

      

create a table of the lower level structure:

CREATE TABLE IF NOT EXISTS `net_downline` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(60) NOT NULL,
  `upline` varchar(60) NOT NULL,
  `level` int(7) NOT NULL,
  `position` varchar(10) NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

      

How to get a simple or basic registration form:

$newuser        = htmlentities(trim($_POST['user']));
$sponsor        = htmlentities(trim($_POST['sponsor']));    
$upline         = htmlentities(trim($_POST['upline']));
$position       = htmlentities(trim($_POST['position']));

      

During the registration process, these tree steps are performed:

// 1. register new member
$users->dummyRegister($newuser, $sponsor, $upline, $position);
// 2. update upline
$users->dummyUpdateUpline($newuser, $upline, $position);

// 3. create donwline structure of binary tree
$level=0;
$memberid=$newuser;
do{
$getdata=$users->dummyGetUpline($memberid); 
$uplne=$getdata[2]; 
$posi=$getdata[3];

$level++;
if($uplne!==''){
$users->dummyInsert_NetDownline($newuser, $uplne, $posi, $level);
}
$memberid=$uplne;
}
while($memberid!='');

      

the user class for this script:

<?php  // start class

    class Users{

        private $db;
        public function __construct($database) {
            $this->db = $database;
        }   
    public function dummyRegister($username, $sponsor, $upline, $position, $today){

            $query  = $this->db->prepare("INSERT INTO `member` (`username`, `sponsor`, `upline`, `position`, `entry_date` ) VALUES (?, ?, ?, ?, ?) ");          
            $query->bindValue(1, $username);
            $query->bindValue(2, $sponsor);
            $query->bindValue(3, $upline);
            $query->bindValue(4, $position);
            $query->bindValue(5, $today);

            try{
                $query->execute();
             }catch(PDOException $e){
                die($e->getMessage());
            }   
        }


    public function dummyUpdateUpline($username, $upline, $position){

            if ($position=='left') {
            $query  = $this->db->prepare("UPDATE `member` SET `_left`=? WHERE username=? ");
            }elseif ($position=='right') {
            $query  = $this->db->prepare("UPDATE `member` SET `_right`=? WHERE username=? ");
            }

            $query->bindValue(1, $username);
            $query->bindValue(2, $upline);

            try{
                $query->execute();

            }catch(PDOException $e){
                die($e->getMessage());
            }   
        }

    public function dummyGetUpline($newuser) {// for demo

            $query = $this->db->prepare("SELECT * FROM `member` WHERE `username`= ?");
            $query->bindValue(1, $newuser);

            try{
                $query->execute();
                $rows = $query->fetch();

                return $rows;//['upline'];

            } catch(PDOException $e){
                die($e->getMessage());
            }
        }

    public function dummyInsert_NetDownline($newuser, $upline, $posi, $level){// for demo

            $query  = $this->db->prepare("INSERT INTO `net_downline` (`username`, `upline`, `position`, `level` ) VALUES (?, ?, ? ,?) ");
            $query->bindValue(1, $newuser);
            $query->bindValue(2, $upline);
            $query->bindValue(3, $posi);
            $query->bindValue(4, $level);

            try{
                $query->execute();

            }catch(PDOException $e){
                die($e->getMessage());
            }   
        }   

    }// endclass

      

create initial.php and put the upper registration php script:

<?php  
if(!isset($_SESSION)) { session_start(); }
require 'conn/database.php'; // in folder conn
require 'clas/users.php';   // in folder class
$users      = new Users($db);
} 
?>

      

to handle the database connection (clas / database.php script):

<?php 
$config = array(
    'host'      => 'localhost',
    'username'  => 'root',
    'password'  => '',
    'dbname'    => 'sampledatabase'
);

$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  

      

+3


source to share


1 answer


This is the answer after many attempts. Solution to my previous problem. Just use the one member table above.

To show the number of bottom lines, left and right. I insert this script into the HTML element tree page for each user in tree A, all the way down to B / C, all the way down to D / E / F / G):

<?php echo $users->downline_number($member,'_left'); ?>
<?php echo $users->downline_number($member,'_right'); ?>

      

Add this function to your 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 shows the structure of the binary tree. The memberID mapping is not tied here, called it in a simple way. Number on the left and bottom right.

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

+1


source







All Articles