我有两个循环,一个调用代理,另一个调用一组日期。日期循环位于代理循环内。目的是计算每个代理的内部行数。我得到的是我需要的每个代理人的总加起来而不是单独的总数。
$company = $_SESSION['user']['company']; $sth = $db->prepare("SELECT id, firstname, lastname, company FROM users WHERE company = '".$company."' AND level = 'user' AND status = 'Active'");
$sth->execute();
$agents = $sth->fetchAll();
foreach($agents as $name){
$begin = new DateTime($date1);
$end = new DateTime($date2);
$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
foreach($daterange as $date){
$salesdates = $date->format("Y-m-d");
$query = "SELECT unid FROM ".$company."_automated_responses WHERE title = 'Sale' AND date LIKE '".$salesdates."%' AND agent_id = '".$name['id']."'";
$stmt = $db->prepare( $query );
$stmt->execute();
$salescount = $stmt->rowCount();
if($salescount < 10){ $bonus = 0; }
else if($salescount < 12){ $bonus = 25; }
else if($salescount < 14){ $bonus = 40; }
else if($salescount < 16){ $bonus = 50; }
else if($salescount < 18){ $bonus = 70; }
else if($salescount < 20){ $bonus = 85; }
else if($salescount > 19){ $bonus = 100; }
$total_bonus += $bonus;
$total_salescount += $salescount;
}
echo $total_salescount.' - '.$name['firstname'].' '.$name['lastname'].' - '.$total_bonus.'<br />';
}
这是输出:
125 - 代理人1 - 115
245 - 代理人2 - 180
387 - 特工3 - 400
517 - 特工4 - 540
它应该是这样的:
125 - 代理人1 - 115
120 - 特工2 - 75
142 - 代理人3 - 220
130 - 特工4 - 140
答案 0 :(得分:1)
你需要设置$ total_salescount = 0;在第一个循环中。这样,当它进入第二个循环时,它将以0开始,而不是继续添加已经存在的东西。
答案 1 :(得分:0)
我想您忘记在$total_bonus
循环开始时将$total_salescount
和foreach($agents as $name)
重置为零。如果没有重置,这些值会在$name
的不同值中不断增长。