问题
I am new to PHP, MySQL and I was practicing on a binary MLM project. The problem is, I want to implement spillover system in this MLM system so that if root user of the tree already has two child "LEFT" & "RIGHT" and if he refers another user to left then it should go to the left empty child's left side and if he refers the new user to right then it should go to the right empty child's right side.
I have tried many function examples from StackOverflow and other resources but I didn't find the result.
Here is the database table user ('id' is primary and AI):
___________________________________
|id |userid | sponser_id | side |
___________________________________
|1 | 11111 | 0 | root |
|2 | 22222 | 11111 | left |
|3 | 33333 | 11111 | right |
___________________________________
Here is the database table tree ('id' is primary and AI):
__________________________________________________________________
|id |userid | left_child | left_child | right_count| right_count |
__________________________________________________________________
|1 | 11111 | 22222 | 33333 | 1 | 1 |
|2 | 22222 | NULL | NULL | 0 | 0 |
|3 | 33333 | NULL | NULL | 0 | 0 |
__________________________________________________________________
Now if userid 11111 refers a new member to left then it should go under 33333.
Here is the register.php code:
<?php
include('php-includes/connect.php');
include('php-includes/check-login.php');
include('ajaxuser.php');
include('php-includes/functions.php');
$userid = $_SESSION['userid'];
$capping = 5;
?>
<?php
//User clicked on join
if(isset($_POST['register'])){
$side='';
$usercode = id_generate();
$pin = mysqli_real_escape_string($con,$_POST['txtActEpinNo']);
$name = mysqli_real_escape_string($con,$_POST['txtName']);
$gname = mysqli_real_escape_string($con,$_POST['txtGName']);
$mobile = mysqli_real_escape_string($con,$_POST['txtMobile']);
$state = mysqli_real_escape_string($con,$_POST['ddlState']);
$network = mysqli_real_escape_string($con,$_POST['network']);
$sponser = mysqli_real_escape_string($con,$_POST['txtSponser']);
$side = mysqli_real_escape_string($con,$_POST['placeSide']);
$password = mysqli_real_escape_string($con,$_POST['txtPassword']);
$plan = plan_check($pin);
$flag = 0;
if($pin!='' && $sponser!='' && $mobile!='' && $network!='' && $side!='' && $name!='' && $gname!='' && $state!='' && $password!=''){
//User filled all the fields.
if(pin_check($pin)){
//Pin is ok
if(mobile_check($mobile)){
//Mobile is ok
if(user_check($sponser)){
//Under userid is ok
if(side_check($sponser,$side)){
//Side check
$flag=1;
}
else{
$next_upline = getLastChildOfLR($sponser, $side);
}
}
else{
//check under userid
echo '<script>alert("Invalid Sponser userid.");</script>';
}
}
else{
//check Mobile
echo '<script>alert("This Mobile Number is already Registered.");</script>';
}
}
else{
//check pin
echo '<script>alert("Invalid pin");</script>';
}
}
else{
//check all fields are fill
echo '<script>alert("Please fill all the fields.");</script>';
}
//Now we are here
//It means all the information is correct
//Now we will save all the information
if($flag==1){
//Insert into User profile
$query = mysqli_query($con,"insert into user(`userid`,`name`,`hof`,`mobile`,`plan`,`mobile_network`,`state`,`password`,`sponser`,`side`)
values('$usercode','$name','$gname','$mobile','$plan','$network','$state','$password','$sponser','$side')");
//Insert into Tree
//So that later on we can view tree.
$query = mysqli_query($con,"insert into tree(`userid`,`name`,`mobile`) values('$usercode','$name','$mobile')");
//Insert to side
$query = mysqli_query($con,"update tree set `$side`='$usercode' where userid='$next_upline'");
//Update pin status to close
$query = mysqli_query($con,"update pin_list set status='close' where pin='$pin'");
//Inset into Icome
$query = mysqli_query($con,"insert into income (`userid`,`name`,`mobile`) values('$usercode','$name','$mobile')");
echo mysqli_error($con);
//This is the main part to join a user\
//If you will do any mistake here. Then the site will not work.
//Update count and Income.
$temp_under_userid = $sponser;
$temp_side_count = $side.'count'; //leftcount or rightcount
$temp_side = $side;
$total_count=1;
$i=1;
while($total_count>0){
$i;
$q = mysqli_query($con,"select * from tree where userid='$temp_under_userid'");
$r = mysqli_fetch_array($q);
$current_temp_side_count = $r[$temp_side_count]+1;
$temp_under_userid;
$temp_side_count;
mysqli_query($con,"update tree set `$temp_side_count`=$current_temp_side_count where userid='$temp_under_userid'");
//income
if($temp_under_userid!=""){
$income_data = income($temp_under_userid);
//check capping
//$income_data['day_bal'];
if($income_data['day_bal']<$capping){
$tree_data = tree($temp_under_userid);
//check leftplusright
//$tree_data['leftcount'];
//$tree_data['rightcount'];
//$leftplusright;
$temp_left_count = $tree_data['leftcount'];
$temp_right_count = $tree_data['rightcount'];
//Both left and right side should at least 1 user
if($temp_left_count>0 && $temp_right_count>0){
if($temp_side=='left'){
$temp_left_count;
$temp_right_count;
if($temp_left_count<=$temp_right_count){
$new_day_bal = $income_data['day_bal']+100;
$new_current_bal = $income_data['current_bal']+100;
$new_total_bal = $income_data['total_bal']+100;
//update income
mysqli_query($con,"update income set day_bal='$new_day_bal', current_bal='$new_current_bal', total_bal='$new_total_bal' where userid='$temp_under_userid' limit 1");
}
}
else{
if($temp_right_count<=$temp_left_count){
$new_day_bal = $income_data['day_bal']+100;
$new_current_bal = $income_data['current_bal']+100;
$new_total_bal = $income_data['total_bal']+100;
$temp_under_userid;
//update income
if(mysqli_query($con,"update income set day_bal='$new_day_bal', current_bal='$new_current_bal', total_bal='$new_total_bal' where userid='$temp_under_userid'")){
}
}
}
}//Both left and right side should at least 1 user
}
//change under_userid
$next_under_userid = getUnderId($temp_under_userid);
$temp_side = getUnderIdPlace($temp_under_userid);
$temp_side_count = $temp_side.'count';
$temp_under_userid = $next_under_userid;
$i++;
}
//Chaeck for the last user
if($temp_under_userid==""){
$total_count=0;
}
}//Loop
//echo mysqli_error($con);
echo '<script>alert("User successffuly registered.");</script>';
}
}
?>
Here functions.php:
<?php
include('php-includes/connect.php');
$userid = $_SESSION['userid'];
//functions
//Valid Pin Check
function pin_check($pin){
global $con,$userid;
$query =mysqli_query($con,"select * from pin_list where pin='$pin' and userid='$userid' and status='open'");
if(mysqli_num_rows($query)>0){
return true;
}
else{
return false;
}
}
//Plan Verification
function plan_check($pin){
global $con;
$data = array();
$query = mysqli_query($con,"select * from pin_list where pin='$pin'");
$result = mysqli_fetch_array($query);
return $result['pin_value'];
}
//Email Check
function mobile_check($mobile){
global $con;
$query =mysqli_query($con,"select * from user where mobile='$mobile'");
if(mysqli_num_rows($query)>0){
return false;
}
else{
return true;
}
}
//User ID Check
function user_check($user){
global $con;
$query =mysqli_query($con,"select * from user where userid='$user'");
if(mysqli_num_rows($query)>0){
return true;
}
else{
return false;
}
}
//Side availablity
function side_check($sponser,$side){
global $con;
$query =mysqli_query($con,"select * from tree where userid='$sponser'");
$result = mysqli_fetch_array($query);
$side_value = $result[$side];
if($side_value==''){
return true;
}
else{
return false;
}
}
//Income Data
function income($userid){
global $con;
$data = array();
$query = mysqli_query($con,"select * from income where userid='$userid'");
$result = mysqli_fetch_array($query);
$data['day_bal'] = $result['day_bal'];
$data['current_bal'] = $result['current_bal'];
$data['total_bal'] = $result['total_bal'];
return $data;
}
//Tree Data
function tree($userid){
global $con;
$data = array();
$query = mysqli_query($con,"select * from tree where userid='$userid'");
$result = mysqli_fetch_array($query);
$data['left'] = $result['left'];
$data['right'] = $result['right'];
$data['leftcount'] = $result['leftcount'];
$data['rightcount'] = $result['rightcount'];
return $data;
}
//Find next under id
function getUnderId($userid){
global $con;
$query = mysqli_query($con,"select * from user where userid='$userid'");
$result = mysqli_fetch_array($query);
return $result['sponser'];
}
//Find next under id place
function getUnderIdPlace($userid){
global $con;
$query = mysqli_query($con,"select * from user where userid='$userid'");
$result = mysqli_fetch_array($query);
return $result['side'];
}
//userid generate
function id_generate(){
global $con;
$generated_id = rand(100000,999999);
$query = mysqli_query($con,"select * from user where userid= '$generated_id'");
if(mysqli_num_rows($query)>0){
id_generate();
}
else{
return $generated_id;
}}
function isMemberExists($mid='0'){
global $con;
$count = mysqli_fetch_array(mysqli_query($con, "SELECT COUNT(*) FROM user WHERE userid='".$mid."'"));
if ($count[0] == 1){
return true;
}else{
return false;
}
}
// Get Next Empty Upline in Tree
function getLastChildOfLR($sponser,$position)
{
$parentid= $sponser;
$childid= getTreeChildId($parentid, $position);
if($childid!="-1"){
$mid=$childid;
} else {
$mid=$parentid[0];
}
$flag=0;
while($mid!=""||$mid!="0")
{
if(isMemberExists($mid))
{
$nextchildid= getTreeChildId($mid, $position);
if($nextchildid=="-1")
{
$flag=1;
break;
}
else {
$mid = $nextchildid;
}
}//if
else
break;
}//while
return $mid;
}
function getTreeChildId($parentid,$position){
global $con;
$cou = mysqli_fetch_array(mysqli_query($con, "SELECT COUNT(*) FROM user WHERE sponser='".$parentid."' AND side='".$position."'"));
$cid = mysqli_fetch_array(mysqli_query($con, "SELECT userid FROM user WHERE sponser='".$parentid."' AND side='".$position."'"));
if ($cou == 1){
return $cid;
}
else
{
return -1;
}
}
?>
Can anyone help ?
来源:https://stackoverflow.com/questions/61532977/implementing-spilover-system-in-mlm-binary-plan-to-join-a-user-on-last-available