2016-06-10 80 views
0

我正在cakephp項目,我要生成的報告。我有兩張表,一張表存儲客戶的價值,另一張表存儲分配給每個用戶的價值。每個領導都有身份。我想增加progressleads的計數器,如果狀態爲0,如果狀態是1,那麼wonleads的計數器應增加等等...在PHP中增加計數器狀態明智foreach

我將兩個表和獲得陣列像這樣=>

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

     ) 

) 

從這個數組我想要一個記錄爲每個用戶與總引出,inprogress線索,贏得領導櫃檯。我試過下面的代碼: -

$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線索卻是露出機會與其他類似的計數器也顯示不正確。任何人都可以幫助我,我在這裏做錯了嗎?

回答

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 
     ) 

) 
+0

感謝您的幫助,但可以ü請告訴我什麼是$結果包含? – user3327608

+0

'$ finalResult'只是一個包含最終結果的數組。檢查陣列格式的更新答案。 –

+0

這是我得到這樣的第一個記錄: - 陣列 ( [14] =>數組 ( [機會] => 1 [值] => 50000 [INPROGRESS] => 1 [贏得] => 0 [失去] => 0 [計費] => 0 [ONHOLD] => 0 [客戶] => 14 [客戶名稱] => ABC ) – user3327608

0

什麼是這樣做背後你的邏輯:

if($newcustid != $custid){ 
    $totalopp++; 
} 

你分配$ newcustid爲null。我認爲這種狀況導致你錯誤的機會價值。

+0

當二者都沒有在那個時候我已經遞增計數器值相同。我想要每個用戶的計數器值。我是新來的PHP。請幫助我 – user3327608