if($customer_id > 0){ $customer_ml = $this->getCustomer_ml($customer_id); if ( !isset($customer_ml['customer_id']) ) { $this -> db -> query("INSERT INTO " . DB_PREFIX . "customer_ml SET customer_id = '" . (int)$customer_id . "', customer_code = '$customer_code', level = '1', p_binary = '" . $p_node . "', p_node = '" . $p_node . "', date_added = NOW()"); // $customer_ml = $this->getCustomer_ml($customer_id); } $totalChild = $this -> getTotalChild($p_node); // $this -> db -> query(" // UPDATE " . DB_PREFIX . "customer_ml SET p_binary = '" . $p_node . "' // WHERE customer_id = " . $customer_id); $type_node = $type_node == '2' ? rand(0,1) : (int)$type_node; echo $type_node."-".$totalChild."-".$p_node; if ( $totalChild > 1 ) { // $lastLeftMember = []; // $lastRightMember = []; $pNode_ml = $this->getCustomer_ml( $p_node ); if( $this->countMLStat($p_node) == 0 ){ $lastLeft = $this->getLastLeftMember( $p_node ); $lastRight = $this->getLastRightMember( $p_node ); $this->addMlStat( $p_node, $lastLeft['customer_id'], $lastRight['customer_id'], 0, 0, $lastLeft['floor'], $lastRight['floor'] ); $this->updateRootMlStatForCustomer( $p_node, $pNode_ml['p_node'], $pNode_ml['p_binary'] ); } // else{ $p_stat = $this-> getMLStat( $p_node ); // $lastLeftMember['customer_id'] = $p_stat['last_left']; // $lastRightMember['customer_id'] = $p_stat['last_right']; // $lastLeftMember['floor'] = $p_stat['left_floor']; // $lastRightMember['floor'] = $p_stat['right_floor']; // } echo "211 ".$type_node."-".$pNode_ml['customer_id']."-".$customer_id."-".$p_stat['p_root']; /*$this->db->query(" UPDATE " . DB_PREFIX . "customer_ml SET `p_binary` = $customer_id WHERE customer_id = $customer_id");*/ $customer_ml = $this->getCustomer_ml( $customer_id ); print_r($customer_ml); if ($type_node == 0 && $customer_id != $p_stat['last_left'] && $customer_ml['left'] == 0) { // update p_binary for new customer $this->db->query(" UPDATE " . DB_PREFIX . "customer_ml SET `p_binary` = " . $p_stat['last_left'] . " WHERE customer_id = $customer_id"); // update left node for parent $this -> db -> query(" UPDATE " . DB_PREFIX . "customer_ml SET `left` = $customer_id WHERE customer_id = " . $p_stat['last_left']); // $lastLeftMember['floor'] = $lastLeftMember['floor']+1; $this->updateAllMlLeftStat( $p_stat['p_root'], $customer_id ); } else if ( $type_node == 1 && $customer_id != $p_stat['last_right'] && $customer_ml['right'] == 0) { // update p_binary for new customer $this->db->query(" UPDATE " . DB_PREFIX . "customer_ml SET `p_binary` = " . $p_stat['last_right'] . " WHERE customer_id = $customer_id"); // update right node for parent $this -> db -> query(" UPDATE " . DB_PREFIX . "customer_ml SET `right` = $customer_id WHERE customer_id = " . $p_stat['last_right']); // $lastRightMember['floor'] = $lastRightMember['floor']+1; $this->updateAllMlRightStat( $p_stat['p_root'], $customer_id ); } } else if ( $totalChild == 1) { $pNode_ml = $this->getCustomer_ml($p_node); if( $this->countMLStat($p_node) == 0 ){ // $pNode_ml = $this->getCustomer_ml($p_node); $lastLeft = $this->getLastLeftMember( $p_node ); $lastRight = $this->getLastRightMember( $p_node ); $this->addMlStat( $p_node, $lastLeft['customer_id'], $lastRight['customer_id'], 0, 0, $lastLeft['floor'], $lastRight['floor'] ); $this->updateRootMlStatForCustomer( $p_node, $pNode_ml['p_node'], $pNode_ml['p_binary'] ); } $p_stat = $this-> getMLStat( $p_node ); // $query = $this->db->query(" // SELECT * FROM " . DB_PREFIX . "customer_ml // WHERE customer_id = " . $p_node); // $parent = $query->row; echo "256 ".$type_node."-".$pNode_ml['customer_id']."-".$customer_id."-".$p_stat['p_root']; if ( $pNode_ml['left'] == 0 && $type_node == 0 ) { // update left node for parent $this -> db -> query(" UPDATE " . DB_PREFIX . "customer_ml SET `left` = $customer_id WHERE customer_id = " . $pNode_ml['customer_id']); $this->updateAllMlLeftStat($p_stat['p_root'], $customer_id); } else { $this -> db -> query(" UPDATE " . DB_PREFIX . "customer_ml SET `right` = $customer_id WHERE customer_id = " . $pNode_ml['customer_id']); $this->updateAllMlRightStat($p_stat['p_root'], $customer_id); } } public function updateAllMlRightStat($p_root, $last_right){ $query = $this->db->query( "UPDATE " . DB_PREFIX . "customer_ml_stat SET last_right = $last_right, right_floor = right_floor + 1 WHERE p_root = $p_root" ); return true; }