Php 按若干标准分配的数额

Php 按若干标准分配的数额,php,Php,考虑到以下事实,我想分配每个代理佣金的价值: 我可以有n个佣金 佣金在代理类上分配,一个代理可以是多个类的一部分 分配的佣金表的结构(我在其中存储我需要分配的佣金): 代理分配的结构: agent_code | agent_class AGENT_1 | CLASS_1 AGENT_2 | CLASS_1 AGENT_3 | CLASS_2 AGENT_1 | GENERAL_CLASS AGENT_2 | GENERAL_CLASS AGENT_3

考虑到以下事实,我想分配每个代理佣金的价值:

  • 我可以有n个佣金
  • 佣金在代理类上分配,一个代理可以是多个类的一部分
分配的佣金表的结构(我在其中存储我需要分配的佣金):

代理分配的结构:

agent_code     | agent_class
AGENT_1    | CLASS_1
AGENT_2    | CLASS_1
AGENT_3    | CLASS_2
AGENT_1    | GENERAL_CLASS
AGENT_2    | GENERAL_CLASS
AGENT_3    | GENERAL_CLASS
以下是我编写的代码:

$stmt_assigned_commission  = $conn_bd->prepare('select * from assigned_commission ');                               
$stmt_comisioane_repartizate->execute(array());                             
$result_stmt_assigned_commission  = $stmt_assigned_commission ->fetchAll();                             
if ( count($result_stmt_assigned_commission ) ) {                               
    foreach($result_stmt_assigned_commission  as $row_assigned_commission ) {                           
        $commission_type[] = $row_assigned_commission ['commission_type'];              
        $assigned_value[]   = $row_assigned_commission ['assigned_value'];              
        $agent_class[]      = $row_assigned_commission ['agent_class'];             
    }                           
} else {                                
        $commission_type    = 0;                
        $assigned_value = 0;                
        $agent_class        = 0;                
}                               
$count_assigned_commission = count($commission_type);                               



$i = 0;                             
while ($i < $count_assigned_commission ) {                              

    $stmt_agent_distribution = $conn_bd->prepare('select agent_code from agent_allocation                           
    where agent_class = :agent_class');                         
    $stmt_agent_distribution->execute(array('agent_class' => $agent_class[$i]));                            
    $result_stmt_agent_distribution = $stmt_agent_distribution->fetchAll();                         
    if ( count($result_stmt_agent_distribution) ) {                         
        foreach($result_stmt_agent_distribution as $row_agent_distribution) {                       
            $agent_distribution[] = $row_agent_distribution['agent_code'];                  
        }                       
    } else {                            
            $agent_distribution = 0;                    
    }                           

    $count_agent_distribution = count($agent_distribution);                         

    $j = 0;                         
    while ($j < $count_agent_distribution) {                            
        $stmt_calculate->bindValue(':agent_code', $agent_distribution[$j]);                     
        $stmt_calculate->bindValue(':commission_type', $commission_type[$i]);                       
        $stmt_calculate->bindValue(':assigned_value', $assigned_value[$i]);                     
        $stmt_calculate->execute();                     
        $j++;                       
    }                           
    $i++;                           
}
它应该返回什么:

agent_code | commission_type | assigned_value
AGENT_1    | energy_type_1   | 1500
AGENT_1    | energy_type_2   | 250
AGENT_1    | energy_type_3   | 750
AGENT_2    | energy_type_1   | 1500
AGENT_2    | energy_type_2   | 250
AGENT_2    | energy_type_3   | 750
AGENT_3    | energy_type_3   | 750

在循环中执行查询时,可以在MySQL连接中执行相同的操作

SELECT
    a.agent_code, c.commission_type, c.assigned_value
FROM
    agent_allocation a
JOIN
    assigned_commission c
ON
    a.agent_class = c.agent_class
ORDER BY 
    a.agent_code, c.commission_type
现在您的代码可以简化为-

$stmt_assigned_commission = $conn_bd->prepare('SELECT a.agent_code, c.commission_type, c.assigned_value FROM agent_allocation a JOIN assigned_commission c ON a.agent_class = c.agent_class ORDER BY a.agent_code, c.commission_type');
$stmt_assigned_commission->execute();                             
$result_stmt_assigned_commission = $stmt_assigned_commission->fetchAll();                             
if (count($result_stmt_assigned_commission)) {                               
    foreach($result_stmt_assigned_commission  as $row) {                           
         echo $row['agent_code'];              
         echo $row['commission_type'];              
         echo $row['assigned_value'];             
    }                           
}
else{                                
//
}                               
看这把小提琴-

结果-


编辑

agent_code | commission_type | assigned_value
AGENT_1    | energy_type_1   | 1500
AGENT_1    | energy_type_2   | 250
AGENT_1    | energy_type_3   | 750
AGENT_2    | energy_type_1   | 1500
AGENT_2    | energy_type_2   | 250
AGENT_2    | energy_type_3   | 750
AGENT_3    | energy_type_3   | 750
因此,这里是一个快速回顾为什么你的重复发生。每次通过您的
while($i<$count\u assigned\u commission)
您都在向
$agent\u distribution[]
添加新值,而不是替换以前的值,因此基本上是这样做的-

// 1st loop
$agent_distribution[] = array("agent_code"=>"AGENT_1");  // New
$agent_distribution[] = array("agent_code"=>"AGENT_2");  // New

// 2nd loop
$agent_distribution[] = array("agent_code"=>"AGENT_1");  // From 1st Loop
$agent_distribution[] = array("agent_code"=>"AGENT_2");  // From 1st Loop
$agent_distribution[] = array("agent_code"=>"AGENT_1");  // New
$agent_distribution[] = array("agent_code"=>"AGENT_2");  // New

// 3rd loop
$agent_distribution[] = array("agent_code"=>"AGENT_1");  // From 1st Loop
$agent_distribution[] = array("agent_code"=>"AGENT_2");  // From 1st Loop
$agent_distribution[] = array("agent_code"=>"AGENT_1");  // From 2nd Loop
$agent_distribution[] = array("agent_code"=>"AGENT_2");  // From 2nd Loop
$agent_distribution[] = array("agent_code"=>"AGENT_1");  // New
$agent_distribution[] = array("agent_code"=>"AGENT_2");  // New
$agent_distribution[] = array("agent_code"=>"AGENT_3");  // New
所以现在当你做最后的
while($j<$count\u agent\u distribution)
时,你要做2次(第一次
while($i<$count\u assigned\u commission)
循环),4次(第二次
while($i<$count\u assigned\u commission)
循环),然后7次(第三次
while($i<$count\u assigned\u commission)
循环)

结果是这样的-

$i = 0;                             
while ($i < $count_assigned_commission ) {    // 3 Times                            

// first time
$stmt_agent_distribution = 'select agent_code from agent_allocation where agent_class = '.$agent_class[0];
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_1");
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_2");

 if ( count($result_stmt_agent_distribution) ) {                         
    foreach($result_stmt_agent_distribution as $row_agent_distribution) {                       
        $agent_distribution[] = $row_agent_distribution['agent_code'];                  
    }                       
}
$count_agent_distribution = count($agent_distribution); // 2 
$j = 0;                         
while ($j < $count_agent_distribution) { 
    // first time                           
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[0]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[0]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[0]);                     
    $stmt_calculate->execute();
    $results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_1","assigned_value"=>1500);

    // second time                           
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[1]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[0]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[0]);                     
    $stmt_calculate->execute();
    $results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_1","assigned_value"=>1500);                     
    $j++;                       
} 

// second time
$stmt_agent_distribution = 'select agent_code from agent_allocation where agent_class = '.$agent_class[1];
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_1");
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_2");
if ( count($result_stmt_agent_distribution) ) {                         
    foreach($result_stmt_agent_distribution as $row_agent_distribution) {                       
        $agent_distribution[] = $row_agent_distribution['agent_code'];                  
    }                       
}
$count_agent_distribution = count($agent_distribution); // 4
$j = 0;                         
while ($j < $count_agent_distribution) { 
    // first time  -- THIS IS A DUPLICATE AS IT IS FROM THE 1ST while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN                         
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[0]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[1]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[1]);                     
    $stmt_calculate->execute();
    $results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_2","assigned_value"=>250);

    // second time  -- THIS IS A DUPLICATE AS IT IS FROM THE 1ST while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN                             
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[1]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[1]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[1]);                     
    $stmt_calculate->execute();
    $results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_2","assigned_value"=>250);

    // third time                           
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[2]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[1]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[1]);                     
    $stmt_calculate->execute();
    $results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_2","assigned_value"=>250);

    // fourth time                           
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[3]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[1]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[1]);                     
    $stmt_calculate->execute(); 
    $results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_2","assigned_value"=>250);

    $j++;                       
} 


// third time
$stmt_agent_distribution = 'select agent_code from agent_allocation where agent_class = '.$agent_class[2];
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_1");
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_2");
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_3");
if ( count($result_stmt_agent_distribution) ) {                         
    foreach($result_stmt_agent_distribution as $row_agent_distribution) {                       
        $agent_distribution[] = $row_agent_distribution['agent_code'];                  
    }                       
}
$count_agent_distribution = count($agent_distribution); // 7                        
$j = 0;                         
while ($j < $count_agent_distribution) { 
    // first time  -- THIS IS A DUPLICATE AS IT IS FROM THE 1ST while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN                             
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[0]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[2]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);                     
    $stmt_calculate->execute();
    $results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_3","assigned_value"=>750);
    // second time  -- THIS IS A DUPLICATE AS IT IS FROM THE 1ST while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN                             
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[1]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[2]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);                     
    $stmt_calculate->execute();            
    $results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_3","assigned_value"=>750);         
    // third time  -- THIS IS A DUPLICATE AS IT IS FROM THE 2ND while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN                             
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[2]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[2]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);                     
    $stmt_calculate->execute();
    $results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_3","assigned_value"=>750);
    // fourth time  -- THIS IS A DUPLICATE AS IT IS FROM THE 2ND while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN                             
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[3]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[2]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);                     
    $stmt_calculate->execute();
    $results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_3","assigned_value"=>750);
    // fifth time                           
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[4]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[2]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);                     
    $stmt_calculate->execute();
    $results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_3","assigned_value"=>750);

    // sixth time                           
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[5]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[2]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);                     
    $stmt_calculate->execute();
    $results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_3","assigned_value"=>750);
    // seventh time                           
    $stmt_calculate->bindValue(':agent_code', $agent_distribution[6]);                     
    $stmt_calculate->bindValue(':commission_type', $commission_type[2]);                       
    $stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);                     
    $stmt_calculate->execute(); 
    $results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_3","assigned_value"=>750);
    $j++;                       
} 

$i++; 

所以现在看起来像

...
while ($i < $count_assigned_commission ) {                              

    $stmt_agent_distribution = $conn_bd->prepare('select agent_code from agent_allocation                           
    where agent_class = :agent_class');                         
    $stmt_agent_distribution->execute(array('agent_class' => $agent_class[$i]));                            
    $result_stmt_agent_distribution = $stmt_agent_distribution->fetchAll();                         
    if ( count($result_stmt_agent_distribution) ) {                         
        foreach($result_stmt_agent_distribution as $row_agent_distribution) {                       
            $agent_distribution[$i][] = $row_agent_distribution['agent_code'];                  
        }                       
    } else {                            
            $agent_distribution[$i] = 0;                    
    }                           

    $count_agent_distribution = count($agent_distribution[$i]);                         

    $j = 0;                         
    while ($j < $count_agent_distribution) {                            
        $stmt_calculate->bindValue(':agent_code', $agent_distribution[$i][$j]);                     
        $stmt_calculate->bindValue(':commission_type', $commission_type[$i]);                       
        $stmt_calculate->bindValue(':assigned_value', $assigned_value[$i]);                     
        $stmt_calculate->execute();                     
        $j++;                       
    }                           
    $i++;                           
}

我仍然建议使用联接查询,因为每当您在循环中执行循环时,您都容易出现这种类型的错误,然后您的结果会在不知道原因的情况下快速扩展。

是的,但佣金类型可以作为类型数转到“n”,并且无论类型数多少,循环都必须为所有人运行。同时,对于本例,agent_分配表中的数据减少。此查询是可伸缩的。看看这个SQLFIDLE版本,它有25个
佣金类型
和25个
代理代码
和79个
代理分配
行,这会产生360个不同的结果。您可以直接在mysql数据库中尝试查询,以验证您的解决方案是否合理、简单且“更干净”。我试试看。我仍然想知道我写的脚本是不是错了,或者是我遗漏了一点……所以我再次查看了你的代码,这是因为在你的
while($I<$count\u assigned\u commission)
循环中,你不断地添加到
$agent\u分发中
,所以每次在
while($j<$count\u agent\u分发中)
循环您选择的重复项。我补充了我的答案。如果你寻找编辑,它会告诉你为什么会这样。如果你寻找实际的修正,它会告诉你如何修正。这就是为什么嵌套循环不好的原因,因为您经常忽略一件事,它会迅速增加结果。我看不到您提到的aditions。我正在用手机浏览,稍后我会用电脑试试。
$agent_distribution[] = $row_agent_distribution['agent_code'];                  
... 
$count_agent_distribution = count($agent_distribution);
...                        
$stmt_calculate->bindValue(':agent_code', $agent_distribution[$j]);
$agent_distribution[$i][] = $row_agent_distribution['agent_code'];                  
... 
$count_agent_distribution = count($agent_distribution[$i]);
...                        
$stmt_calculate->bindValue(':agent_code', $agent_distribution[$i][$j]);
...
while ($i < $count_assigned_commission ) {                              

    $stmt_agent_distribution = $conn_bd->prepare('select agent_code from agent_allocation                           
    where agent_class = :agent_class');                         
    $stmt_agent_distribution->execute(array('agent_class' => $agent_class[$i]));                            
    $result_stmt_agent_distribution = $stmt_agent_distribution->fetchAll();                         
    if ( count($result_stmt_agent_distribution) ) {                         
        foreach($result_stmt_agent_distribution as $row_agent_distribution) {                       
            $agent_distribution[$i][] = $row_agent_distribution['agent_code'];                  
        }                       
    } else {                            
            $agent_distribution[$i] = 0;                    
    }                           

    $count_agent_distribution = count($agent_distribution[$i]);                         

    $j = 0;                         
    while ($j < $count_agent_distribution) {                            
        $stmt_calculate->bindValue(':agent_code', $agent_distribution[$i][$j]);                     
        $stmt_calculate->bindValue(':commission_type', $commission_type[$i]);                       
        $stmt_calculate->bindValue(':assigned_value', $assigned_value[$i]);                     
        $stmt_calculate->execute();                     
        $j++;                       
    }                           
    $i++;                           
}
// 1st loop
$agent_distribution[0][] = array("agent_code"=>"AGENT_1");
$agent_distribution[0][] = array("agent_code"=>"AGENT_2");

// 2nd loop  - does not have the rows from the 1st loop
$agent_distribution[1][] = array("agent_code"=>"AGENT_1");
$agent_distribution[1][] = array("agent_code"=>"AGENT_2");

// 3rd loop - does not have the rows from the 1st and 2nd loops
$agent_distribution[2][] = array("agent_code"=>"AGENT_1");
$agent_distribution[2][] = array("agent_code"=>"AGENT_2");
$agent_distribution[2][] = array("agent_code"=>"AGENT_3");