user3327608 user3327608 - 5 months ago 23
MySQL Question

increment counter status wise in php foreach

I am working on cakephp project where i've to generate the reports. I've two tables one table stores the value of customer and another table store the value of leads assigned to each user. Each lead is having status. I want to increment the counter of progressleads if status is 0, if status is 1 then counter of wonleads should be increment and so on...

I am joining two tables and getting array like this =>

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] =>
)

)

)


From this array i want one record for each users with total leads, inprogress leads, won leads counter. I've tried following code :-

$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;

}


After printing $customerdetails array i am getting following results =>

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
)

)


As you can see there are only 4 leads assigned to ABC but it is showing opportunities as 6 similarly the other counters are also displaying incorrect. Can anyone help me what i am doing wrong here?

Answer

I think you're applying wrong logic here. array_column will do the trick. See the following code.

<?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"]++;
      }     
}

?>

You just need to get the customer column and opportunity column individually and then traverse customers column.

Now when you're traversing, first check if you've already find that customer or not by saving values in $finalResult array. If yes then just increment the counters accordingly if not create an array then increments the counter accordingly.

I think this will do the trick.

Output array might look like this:

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
        )

)
Comments