Я новичок в PHP, MySQL, и я тренировался в бинарном проекте MLM. Проблема заключается в том, что я хочу реализовать в этой системе MLM вторичную систему, чтобы, если у пользователя root дерева уже было два дочерних элемента «ВЛЕВО» и «ВПРАВО», и если он отсылает другого пользователя влево, то go левый пустой левый дочерний элемент, и если он направляет нового пользователя вправо, то он должен go к правому пустому правому дочернему элементу.
Я пробовал много примеров функций из StackOverflow и других ресурсов, но я не сделал t найти результат.
Вот таблица базы данных user
('id' является первичной и AI):
___________________________________
|id |userid | sponser_id | side |
___________________________________
|1 | 11111 | 0 | root |
|2 | 22222 | 11111 | left |
|3 | 33333 | 11111 | right |
___________________________________
Вот таблица базы данных tree
('id' является первичным и 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 |
__________________________________________________________________
Теперь, если userid
11111 ссылается на нового члена слева, тогда он должен go под 33333.
Вот регистр. php код:
<?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>';
}
}
?>
Здесь 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;
}
}
?>
Может кто-нибудь помочь?