我正在开发cakephp项目,我将在那里生成报告。我有两个表,一个表存储客户的值,另一个表存储分配给每个用户的潜在客户的值。每个线索都有状态。如果status为0,我想增加progressleads的计数器,如果status为1,那么wonleads的计数器应该递增,等等......
我正在加入两个表并获得像这样的数组=>
Array
(
[0] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)
[Opportunity] => Array
(
[status] => 0
[value] => 50000
)
)
[1] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)
[Opportunity] => Array
(
[status] => 1
[value] => 10000
)
)
[2] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)
[Opportunity] => Array
(
[status] => 1
[value] => 7500
)
)
[3] => Array
(
[Customer] => Array
(
[id] => 15
[name] => DEF
)
[Opportunity] => Array
(
[status] => 0
[value] => 45000
)
)
[4] => Array
(
[Customer] => Array
(
[id] => 19
[name] => ST
)
[Opportunity] => Array
(
[status] => 2
[value] => 50000
)
)
[5] => Array
(
[Customer] => Array
(
[id] => 16
[name] => TEST
)
[Opportunity] => Array
(
[status] => 2
[value] => 1000000
)
)
[6] => Array
(
[Customer] => Array
(
[id] => 19
[name] => ST
)
[Opportunity] => Array
(
[status] => 0
[value] => 1000
)
)
[7] => Array
(
[Customer] => Array
(
[id] => 14
[name] => ABC
)
[Opportunity] => Array
(
[status] => 0
[value] =>
)
)
)
从这个数组中,我希望每个用户有一条记录,总线数,进展线索,赢得线索计数器。我试过以下代码: -
$customerdetails = array();
$totalopp = 0;
$progressopp = 0;
$oppval = 0;
$wonopp = 0;
$lostopp = 0;
$billedopp = 0;
$onholdopp = 0;
$newcustid = NULL;
foreach($customer as $k => $val){
$custid = $val["Customer"]["id"];
if($newcustid != $custid){
$oppstatus = $val["Opportunity"]["status"];
$oppval += $val["Opportunity"]["opo_value"];
$totalopp++;
if($oppstatus == 0){
$progressopp++;
}
if($oppstatus == 1){
$wonopp++;
}
if($oppstatus == 2){
$lostopp++;
}
if($oppstatus == 3){
$billedopp++;
}
if($oppstatus == 4){
$onholdopp++;
}
$newcustid = $custid;
}
$customerdetails[$custid]["customername"] = $val["Customer"]["customer_name"];
$customerdetails[$custid]["opportunities"] = $totalopp;
$customerdetails[$custid]["value"] = $oppval;
$customerdetails[$custid]["inprogress"] = $progressopp;
$customerdetails[$custid]["won"] = $wonopp;
$customerdetails[$custid]["lost"] = $lostopp;
$customerdetails[$custid]["billed"] = $billedopp;
$customerdetails[$custid]["onhold"] = $onholdopp;
}
打印$ customerdetails数组后,我得到以下结果=>
Array
(
[14] => Array
(
[customername] => ABC
[opportunities] => 6
[value] => 1146000
[inprogress] => 4
[won] => 0
[lost] => 2
[billed] => 0
[onhold] => 0
)
[15] => Array
(
[customername] => DEF
[opportunities] => 2
[value] => 95000
[inprogress] => 2
[won] => 0
[lost] => 0
[billed] => 0
[onhold] => 0
)
[19] => Array
(
[customername] => ST
[opportunities] => 5
[value] => 1146000
[inprogress] => 3
[won] => 0
[lost] => 2
[billed] => 0
[onhold] => 0
)
[16] => Array
(
[customername] => TEST
[opportunities] => 4
[value] => 1145000
[inprogress] => 2
[won] => 0
[lost] => 2
[billed] => 0
[onhold] => 0
)
)
正如您所看到的,只有4条线索分配给ABC,但它显示的机会与6相似,其他计数器也显示不正确。谁能帮助我,我在这里做错了什么?
答案 0 :(得分:1)
我认为你在这里应用了错误的逻辑。 array_column会做到这一点。请参阅以下代码。
<?php
$customers=array_column($result,'customer');
$opportunity=array_column($result,'opportunity');
$finalResult=array();
foreach($customers as $k=>$customer) {
$opp=$opportunity[$k];
if(!array_key_exists($customer["id"],$finalResult)) {
$finalResult[$customer["id"]]=array();
$finalResult[$customer["id"]]["opportunities"]=0;
$finalResult[$customer["id"]]["value"]=0;
$finalResult[$customer["id"]]["inprogress"]=0;
$finalResult[$customer["id"]]["won"]=0;
$finalResult[$customer["id"]]["lost"]=0;
$finalResult[$customer["id"]]["billed"]=0;
$finalResult[$customer["id"]]["onhold"]=0;
}
$finalResult[$customer["id"]]["customerid"]=$customer["id"];
$finalResult[$customer["id"]]["customername"]=$customer["name"];
$finalResult[$customer["id"]]["opportunities"]++;
$finalResult[$customer["id"]]["value"]+=$opp["value"];
if($opp["status"]==0) {
$finalResult[$customer["id"]]["inprogress"]++;
}
if($opp["status"]==1) {
$finalResult[$customer["id"]]["won"]++;
}
if($opp["status"]==2) {
$finalResult[$customer["id"]]["lost"]++;
}
if($opp["status"]==3) {
$finalResult[$customer["id"]]["billed"]++;
}
if($opp["status"]==4) {
$finalResult[$customer["id"]]["onhold"]++;
}
}
?>
您只需要单独获取客户列和机会列,然后遍历客户列。
现在,当您进行遍历时,首先通过在$finalResult
数组中保存值来检查您是否已找到该客户。如果是,那么只需相应地增加计数器,如果不创建数组,则相应地增加计数器。
我认为这样可以解决问题。
输出数组可能如下所示:
Array
(
[14] => Array
(
[opportunities] => 4
[value] => 13500
[inprogress] => 2
[won] => 2
[lost] => 0
[billed] => 0
[onhold] => 0
[customerid] => 14
[customername] => ABC
)
[15] => Array
(
[opportunities] => 1
[value] => 45000
[inprogress] => 1
[won] => 0
[lost] => 0
[billed] => 0
[onhold] => 0
[customerid] => 15
[customername] => DEF
)
[19] => Array
(
[opportunities] => 2
[value] => 51000
[inprogress] => 1
[won] => 0
[lost] => 1
[billed] => 0
[onhold] => 0
[customerid] => 19
[customername] => ST
)
[16] => Array
(
[opportunities] => 1
[value] => 100000
[inprogress] => 0
[won] => 0
[lost] => 1
[billed] => 0
[onhold] => 0
[customerid] => 16
[customername] => TEST
)
)
答案 1 :(得分:0)
这背后的逻辑是什么:
if($newcustid != $custid){
$totalopp++;
}
您将$ newcustid指定为null。我认为这种情况会导致错误的机会价值。