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

时间:2015-02-16 08:52:00

标签: php mysql

我想从给定的代理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已被弃用 - 它只是用于测试。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我通过以下代码得到了我想要的输出@Danyal Sandeelo指导和referred

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有直接的introducer_id 10,所以它有相应级别的佣金,但是有6个中间级别在6到10之间(7,8,9)。现在我想添加这些级别&#39;佣金金额为10级佣金。我如何使用上面发布的相同功能进行操作。 帮助我!!