Codeigniter二叉树(MLM)1:2概念父级插入

时间:2018-04-13 11:38:40

标签: codeigniter binary-tree

this is my member_relation (binary tree table)

this is my tree panrent level table

this is my tree structure

当树父节点满足两个子节点时,我需要插入二叉树父级别,如果它已满足它到达第一级...同样如果它们的两个子节点在它们的下行满足两个子节点。父级达到第二级......当树长大时,这个过程会重复。

PLZ有人建议我如何为这个级别插入编写逻辑吗?

1 个答案:

答案 0 :(得分:0)

public function membercode($MembershipID=null)

{   
$this->load->model('MemberShip_Relation_Model');
$MembershipID=$this->input->post('mid');
$New=$this->MemberShip_Relation_Model->getnewid($MembershipID);
$NewMemberid=$New->NewregID;
$city_id=$this->input->post('city');
$state_id=$this->input->post('state');
$status_id=$this->input->post('status');
$sponser_id=$this->input->post('sponser');
$this->load->model('Registration_model');
$code=$this->Registration_model->code_count($city_id,$state_id);
$template=$this->Registration_model->member_code($MembershipID);
$code_gen=$code->count+1;
$state_code=$template->state_code;
$city_code=$template->city_code;
$pmt='PMT';

if ($code_gen<=9) {

$memcode = $pmt.'-'.$state_code.'-'.$city_code.'-00'.$code_gen ;

$voucherscode='00'.$code_gen;

}

elseif ($code_gen>=10&&$code_gen<=99) {

$memcode = $pmt.'-'.$state_code.'-'.$city_code.'-0'.$code_gen ;

$voucherscode='0'.$code_gen;

}
else{

$memcode = $pmt.'-'.$state_code.'-'.$city_code.'-'.$code_gen ;

$voucherscode=$code_gen;

}

$memcount = $this->MemberShip_Relation_Model->memrcount();

$memcountz=$memcount[0];

$memtotcount=$memcountz->count +1;


$member_code=array(

'MembershipCode'=>$memcode,
'RegistrationStatusID'=>$status_id,
'Position'=>$memtotcount
               );


$this->db->where('MembershipID',$MembershipID);
$this->db->update('membership_table',$member_code); 

$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#$%';
$password = array(); 
$alpha_length = strlen($alphabet) - 1; 
for ($i = 0; $i < 8; $i++) 
{
$n = rand(0, $alpha_length);
$password[] = $alphabet[$n];
}
$test1= implode($password); 
$password=md5($test1);

$user_login=array(

'MembershipID'=>$MembershipID,
'UserName'=>$memcode,
'Password'=>$password,
'PasswordAnswer'=>$test1,
'type'=>'staff'           

);


$this->db->insert('userlogin_table',$user_login);


date_default_timezone_set("Asia/Kolkata");
$for=date('Y-m-d');

$voucheramnt1=$this->MemberShip_Relation_Model->voucher1();
$voucheramnt2=$this->MemberShip_Relation_Model->voucher2();
$voucheramnt3=$this->MemberShip_Relation_Model->voucher3();

$va1=$voucheramnt1->amount;
$va2=$voucheramnt2->amount;
$va3=$voucheramnt3->amount;

for ($i=0; $i<=6; $i++) { 
$n_tbl[]=
array(

'val_date'=>date('Y-m-d',strtotime("$for +$i days")),

    ); 

$n_tbl[$i]['MembershipID']=$MembershipID;


}


$this->db->insert_batch('notification_table',$n_tbl);




$today=date('Y-m-d');
$next_date= date('Y-m-d', strtotime($today. ' + 45 days'));



$voucher1=array(

'VoucherName'=>'PMT'.$voucherscode.$va1,
'VoucherAmount'=>$va1,
'Validityfrom'=>$today,
'validityto'=>$next_date,
'MembershipID'=>$MembershipID,
'usedstatus'=>'0',
'createdby'=>'admin',
'createdon'=>$for
);


$this->db->insert('voucher_table',$voucher1);


$voucher2=array(

'VoucherName'=>'PMT'.$voucherscode.$va2,
'VoucherAmount'=>$va2,
'Validityfrom'=>$today,
'validityto'=>$next_date,
'MembershipID'=>$MembershipID,
'usedstatus'=>'0',
'createdby'=>'admin',
'createdon'=>$for
);



$this->db->insert('voucher_table',$voucher2);


$voucher3=array(

'VoucherName'=>'PMT'.$voucherscode.$va3,
'VoucherAmount'=>$va3,
'Validityfrom'=>$today,
'validityto'=>$next_date,
'MembershipID'=>$MembershipID,
'usedstatus'=>'0',
'createdby'=>'admin',
'createdon'=>$for
);


$this->db->insert('voucher_table',$voucher3);

$getemail=$this->MemberShip_Relation_Model->getemail($MembershipID);


$email=$getemail->EMail;
$name=$getemail->Name;
$membercode=$getemail->MembershipCode;
$sponsercode=$getemail->SponsershipID;

$config = array(

'mailtype' => 'html',

);


$atrr=array(

'email'=>$email,

);


$message = '
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h3>Hi '.$name.'-('.$memcode.') Welcome to Planmitrip-Travellersclub</h3>

<p>Your UserName,Password and Vouchercodes are mentioned below</p>

<table>
<tr>
<td><b>UserName :</b></td>
<td>'.$memcode.'</td>
</tr>
<tr>
<td><b>Password :</b></td>
<td>'.$test1.'</td>
</tr>
<tr>
<td><b>Promoter Code:</b></td>
<td>'.$sponsercode.'</td>
</tr>
<tr>
<td><b>Membership Code:</b></td>
<td>'.$memcode.'</td>
</tr>
<tr>
<td><b>Vouchercode1  :</b></td>
<td>PMT'.$voucherscode.$va1.'</td>
</tr>
<tr>
<td><b>Vouchercode2  :</b></td>
<td>PMT'.$voucherscode.$va2.'</td>
</tr>
<tr>
<td><b>Vouchercode3  :</b></td>
<td>PMT'.$voucherscode.$va3.'</td>
</tr>



</table>
<h4>Thank you,</h4>
<p>Regards Planmitrip</p>
</body>
</html>

';

$this->load->helper('date');
$this->load->library('email', $config);
$this->email->set_mailtype("html");

$from = $this->email->from('info@planmitrip.in');
$this->email->to($email);

$this->email->subject('Travellersclub-Registration');
$this->email->message($message);

$this->email->send($from);


if($status_id == 3){

// Tree insert coding

$this->load->model('Registration_model');
$this->load->model('MemberShip_Relation_Model');
$template = $this->MemberShip_Relation_Model->getSponsershipID($MembershipID);
$parent_member = $this->MemberShip_Relation_Model->getParentMemberID($template->SponsershipID);
$relation = $this->MemberShip_Relation_Model->getRelationByMemberId($parent_member->MembershipID);
$parent = $this->MemberShip_Relation_Model->getParents1($parent_member->MembershipID);
$parent1 = $this->MemberShip_Relation_Model->getParents($parent_member->MembershipID);
if(count($parent) == 1){
$leaf = array('parent_id'=>$parent_member->MembershipID,'leg' =>1);
}elseif(empty($parent)){
$leaf = array('parent_id'=>$parent_member->MembershipID,'leg' =>0);
}
else{
$branch = $this->MemberShip_Relation_Model->buildTree($parent1,$parent_member->MembershipID);
$this->MemberShip_Relation_Model->setBranch($branch);
$this->MemberShip_Relation_Model->buildLevelTree($branch);
$levels = $this->MemberShip_Relation_Model->getLevelArray();
$branchArray = $this->MemberShip_Relation_Model->getBranchArray();
$leaf = $this->MemberShip_Relation_Model->getLeaf();
}
if(is_array($leaf)){
$parent_id = $leaf['parent_id'];
$leg = $leaf['leg'];
}
else{
$parent_id = 0;
$leg = 0;
}

$tot=count($leg==1);
$parent = $this->MemberShip_Relation_Model->getParentIdByChild($parent_id);
if(!empty($parent)){
$parent_ids = $parent->parent_ids.$parent_id.',';  
} else {
$parent_ids = '0,';
} 
$parentList = explode(',',$parent_ids);
$parentListCount = count($parentList) - 2;
if(empty($NewMemberid)){
$MemberRelationID = $this->MemberShip_Relation_Model->insertRelationship($MembershipID,$parent_id,$leg,$parent_ids);
}
else{
$replace=array(
'ChildMemberID'=>$MembershipID,
);
$this->db->where('ChildMemberID',$NewMemberid);
$this->db->update('memberrelationship_table',$replace);
$stchange=array(
'ReplacedStatus'=>null,
);
$this->db->where('MembershipID',$MembershipID);
$this->db->update('membership_table',$stchange);
$this->db->query("delete from userlogin_table where MembershipID='{$NewMemberid}'");
$this->db->query("delete from voucher_table where MembershipID='{$NewMemberid}'");
$this->db->query("delete from notification_table where MembershipID='{$NewMemberid}'");
$this->db->query("delete from notification_tablefinal where MembershipID='{$NewMemberid}'");
}

// Tree insert coding ends

// Level insert coding starts

$get_leg=$this->MemberShip_Relation_Model->get_legs($MembershipID);
$leg_check=$get_leg->leg;
$parent_node=$get_leg->parent_ids;
if($leg_check==1){
$parent_nodes = explode(',',$parent_node);
$final_parent_ids=array_reverse($parent_nodes);
$parent_count=count($parent_nodes) - 2;
// error_log(print_r($final_parent_ids,true),3,$_SERVER['DOCUMENT_ROOT']."PlanmitripLatest/finallevel1.log");
for($i=1;$i<=$parent_count; $i++){
$immediate_child=$this->MemberShip_Relation_Model->getChildvalues($final_parent_ids[$i]);
$j=0;
foreach ($immediate_child as  $value){ 
$child_ids=$value->ChildMemberID;
$get_parentLevels = $this->MemberShip_Relation_Model->getLevelID($child_ids);
if($j==1){
$first_child_current_level=$get_parentLevels->LevelID;
}
$second_child_current_level=$get_parentLevels->LevelID;
if($first_child_current_level==$second_child_current_level){
$tree_current_level=$get_parentLevels->LevelID;
$levelID=$tree_current_level+1;
}

$j++;
}
$get_parentLevels_new = $this->MemberShip_Relation_Model->getLevelID($final_parent_ids[$i]);
$new_level=$get_parentLevels_new->LevelID;
if($levelID==$new_level) {

}
else {
$TransactionID = $this->MemberShip_Relation_Model->inserttransaction($MembershipID,$final_parent_ids[$i]);
$commissionID = $this->MemberShip_Relation_Model->insertcommission($MembershipID,$final_parent_ids[$i],$TransactionID);
$memberlevelID = $this->MemberShip_Relation_Model->insertmemberlevel($MembershipID,$final_parent_ids[$i],$TransactionID,$commissionID,$levelID);

$commission = $this->MemberShip_Relation_Model->CommissionUpdate($commissionID);

$trans = $this->MemberShip_Relation_Model->TransactionUpdate($TransactionID);

$trans_level= $this->MemberShip_Relation_Model->getMemdetails($memberlevelID);

$tmemid=$trans_level->MembershipID;
$tlevid=$trans_level->LevelID;

$lev_amnt =$this->MemberShip_Relation_Model->getLevelAmnt($tlevid);
$notifi_code=array(

'Level_status'=>'1',

            );

$this->db->where('MembershipID',$MembershipID);
$this->db->update('notification_table',$notifi_code);

$notifi_finalcode=array(

 'status'=>'1',                    
  );


$this->db->where('commonid',$tmemid);
$this->db->update('notification_tablefinal',$notifi_finalcode);

$tran=$trans->LevelAmount;
$comm=$commission->LevelAmount;

$trans_code=array(

'TransactionAmnt'=>$tran,
'TransactionDescription'=>'Level '.$tlevid.' Commission',

               );


$this->db->where('TransactionID',$TransactionID);
$this->db->update('transaction_table',$trans_code);

$comm_code=array(

'CommissionAmnt'=>$comm,
'Commissionfor'=>'Level '.$tlevid.' Commission',

               );


$this->db->where('CommissionID',$commissionID);
$this->db->update('commission_table',$comm_code);

$trans_level= $this->MemberShip_Relation_Model->getMemdetails($memberlevelID);

$tmemid=$trans_level->MembershipID;
$tlevid=$trans_level->LevelID;

$lev_amnt =$this->MemberShip_Relation_Model->getLevelAmnt($tlevid);

$lamnt=$lev_amnt->LevelAmount;

date_default_timezone_set("Asia/Kolkata");
$for=date('Y-m-d');
$trans_insert=array(

'MembershipID'=>$tmemid,
'LevelID'=>$tlevid,
'LevelAmnt'=>$lamnt,
'DeductionAmnt'=>'0',
'TransferedAmnt'=>'0',
'WalletAmnt'=>'0',
'created_on'=>$for,
'created_by'=>'admin',
);

$this->db->insert('transaction_level',$trans_insert);


$trans_history=array(


'MembershipID'=>$tmemid,
'description'=>'Commission Amount',
'credit'=>$lamnt,
'debit'=>'0',
'trans_date'=>$for,
'status'=>'1'

);
$this->db->insert('transaction_history',$trans_history);

}

}

}

// Level insert coding ends

} 

$this->session->set_flashdata('message',  array('message' => 'Edit status details Updated Successfully','class' => 'success'));
redirect("admin/Viewpromoters");

}