2014-06-14 4 views
0

클라이언트 이름 목록을 반복하여 서비스에 사용 된 총 시간 (분)을 가져옵니다. 문제는 각 클라이언트가 둘 이상의 데이터베이스를 가지고 있기 때문에 이들을 반복해야하고 각 클라이언트와 관련된 합계를 추가해야한다는 것입니다. 각 클라이언트에 대한 결과를 추가 할 수 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?php - foreach 루프에서 클라이언트 목록에 대한 합계를 더하기

PHP

foreach ($clients as $client) { 
    foreach ($dbNew as $d) { 
     $result += (strtotime($closing_time[$x]->log_time) - strtotime($opening_time[$x]->log_time)); 

     print "<pre>"; 
     print_r($client); 
     print " --- "; 
     print_r($result); 
     print "</pre>"; 
    } 
} 

출력 (미약 한 시도를) 클라이언트 합계를 얻을 수

[email protected]_rr.com --- 425 
[email protected]_rr.com --- 225 
doug --- 0 
doug --- 0 
[email protected] --- 0 
[email protected] --- 4357 
[email protected] --- 336 

PHP :이에서

$client_result = 0; 
if($client == $client) 
$client_result = $result + $result; 

결과가 올바르지 않습니다.

답변

1

내가 올바르게 질문을하면 각 클라이언트는 다른 시간에 액세스하고 다른 출처에 넣습니다. 그럼 그 경우에 u는 클라이언트의 고유 한 필드 (전자 메일과 같은)를 사용하여 다른 필드를 작성할 수 있습니다.

$res = array(); 
foreach ($clients as $client) { 
    foreach ($dbNew as $d) { 
     $result += (strtotime($closing_time[$x]->log_time) - strtotime($opening_time[$x]->log_time)); 

     print "<pre>"; 
     print_r($client); 
     if(isset($res[$client[0]])){ 
     //i guessed the first field of $client is the email 
     //if the client is already added to the $res list add the current time to it 
      $res[$client[0]] += $result; 
     }else{ 
     //else this is the first instance of the client.... 
      $res[$client[0]] = $result; 
     } 
     print " --- "; 
     print_r($result); 
     print "</pre>"; 
    } 
} 

여기에 $res을 인쇄 할 수 있습니다!

+0

그게 전부 야! $ 클라이언트에서 "[0]"을 사용할 필요는 없지만 노트는 큰 도움이되었습니다. 고맙습니다! –

+0

언제든지 ... 언제든지 –

관련 문제