递归函数返回数组值,在php中该函数之外的sql查询中使用该值


Recursive function returns array values use that values in sql query outside that function in php

我想从给定的代理id中获得赞助商级别。所以我使用以下代码:

function categoryChild($agent,$property) {
global $sponser;
$s = "SELECT c1.Level,c1.id,c1.sponsor_id,c1.sponser_level FROM agent_details c1 WHERE  c1.id='".$agent."'";
//echo $s;
$r = mysql_query($s);
// echo  $user_level=$fetch['sponser_level'];
 //echo $intro_id;
$sponser =array();
if(mysql_num_rows($r) > 0) {
    //echo "hai";
    # It has children, let's get them.
    while($fetch=mysql_fetch_assoc($r)) {
        # Add the child to the list of children, and get its subchildren
       // $sponser [$fetch['id']] = categoryChild($fetch['sponsor_id'],$property);
       $sponser[] = categoryChild($fetch['sponsor_id'],$property);
       // commissionCal($property,$sponser);
    }
    print_r($sponser); // this returns Array ( [0] => Array ( ) ) Array ( [0] => Array ( ) [1] => Array ( [0] => Array ( ) ) ) 
    }
  return  $sponser;
 }
 function commissionCal($property,$sponser_level)
  {
//print_r($sponser_level);
     $get_level="SELECT a1.sponser_level,a2.agent_level_id,a2.property,a2.percentage FROM agent_details a1,agent_level_amount a2 WHERE a2.property='".$property."' AND a2.agent_level_id='". $sponser_level."' ";
  }
 function callFunction($agent,$property)
 {
     $sponser_level=categoryChild($agent,$property);
    //print_r($sponser_level);  
   commissionCal($property,$sponser_level);
  }
  callFunction($agent,$property);

我想在下一个函数中获得赞助商的级别,该函数具有选择查询来检索相应级别的百分比值。

请不要告诉MySQL已经被弃用了——它只是为了测试。

@变量留在存储过程或存储函数之后。将它们分配到函数内部,然后在外部(或在下一个函数中)使用。

我已经通过@Danyal Sandeelo指南从以下代码中获得了我想要的输出,并参考了

function categoryChild($sponser,$id,$last_ins_id,$property,$total_amount,$date) {
global $sponser;
$s = "SELECT c1.Level,c1.id,c1.sponsor_id,c1.sponser_level FROM agent_details c1 WHERE c1.id='".$id."'";
//echo $s;
$r = mysql_query($s);
// echo  $user_level=$fetch['sponser_level'];
if(mysql_num_rows($r) > 0) {
    # It has children, let's get them.
    while($fetch=mysql_fetch_assoc($r)) {
      $sponsor_id = $fetch['sponsor_id'];
      $agent_id=$fetch['id'];
      $sponser[] = $fetch['sponser_level'];
    //print_r($sponser);
     $c=count($sponser);
     //echo $c."<br>";
     foreach($sponser as $key=>$val)
     {
        // echo $key.'-'.$val."<br>";
         $level=$val;
     }
     //$property=1;
      $get_level="SELECT a1.sponser_level,a2.agent_level_id,a2.property,a2.percentage FROM agent_details a1,agent_level_amount a2 WHERE a2.property='".$property."' AND a2.agent_level_id='". $level."' ";
      //echo $get_level;
      $exe_query=mysql_query($get_level);
      $fetch_percentage=mysql_fetch_array($exe_query);
      $per=$fetch_percentage['percentage'];
      $property_id=$fetch_percentage['property'];
      $amount=$total_amount*$per/100;
      //echo $per."<br>";
      $ins="INSERT INTO `agent_commission` (`agentid`, `bookid`,`property_id`,`commission`,`commission_amount`,`date`,`status`) VALUES ('".$agent_id."', '".$last_ins_id."','".$property_id."', '".$per."','".$amount."','".$date."', 'pending');";
      //echo $ins;
      $exe_ins=mysql_query($ins);
          categoryChild($sponser,$fetch['sponsor_id'],$last_ins_id,$property,$total_amount,$date);
       // commissionCal($property,$sponser);
        }
    }
  }
 //$agent=11;
  function getLevels($agent,$last_ins_id,$property,$total_amount,$date)
  {
    $sponser =array();
  categoryChild($sponser,$agent,$last_ins_id,$property,$total_amount,$date);
   return $sponser;
 }
//getLevels($agent); 

从上面的代码中,我得到了以下记录:

    id agentid  bookid  property_id     commission  commission_amount   date    status 
    -------------------------------------------------------------------
    67  14      27        1              10        4000     2015-02-17  pending
    68  10      27        1              10       4000      2015-02-17  pending
    69  1       27        1             0         0         2015-02-17  pending
 /*66   14  27  1   2   800     2015-02-17  pending*/

在上面的结果id 66和67中,具有相同的agent_id-14,其级别为6,所以当级别为6时,其佣金百分比应该为2,但它有2和10。我只想将佣金百分比存储为2。我怎么得到它!!

同时agent_id 14有直接介绍人_id 10,所以它有相应级别的佣金,但在6和10之间有3个中间级别(7,8,9)。现在我想把这些级别的佣金金额加上10级佣金。我如何在上面发布的相同功能中做到这一点。帮帮我!!