Reputation: 5792
I want to show one agent per row with all appointments, call backs, etc...
I tried to put in for loop within for loop. But, It's not giving me correct result.
CODE:
foreach($agents_summary as $as){
<tr>
<td><?=$as['index']?></td>
... etc...
</tr>
ARRAY SAMPLE:
Array
(
[0] => Array
(
[agent_id] => 1
[first_name] => Ronak
[last_name] => Patel
[transaction_type] => APPOINTMENT
[transaction_count] => 5
)
[1] => Array
(
[agent_id] => 1
[first_name] => Ronak
[last_name] => Patel
[transaction_type] => CALL_BACK
[transaction_count] => 5
)
.....
AND SO ON...
EDIT: DATABASE QUERY:
SELECT
a.id AS agent_id,
a.first_name AS first_name,
a.last_name AS last_name,
ct.transaction_type AS transaction_type,
count(*) AS transaction_count
FROM agent AS a
INNER JOIN client_transaction AS ct
ON a.id=ct.generated_by_id
WHERE ct.transaction_type = 'APPOINTMENT'
OR ct.transaction_type = 'CALL_BACK'
OR ct.transaction_type = 'SENT_EMAIL'
OR ct.transaction_type = 'SALE'
GROUP BY agent_id, transaction_type
ORDER BY `agent_id` ASC, `transaction_type` ASC
Upvotes: 1
Views: 45
Reputation: 140
GROUP BY agent_id
at the end of your SQL request
EDIT: There would be a PHP solution with a for loop that would just merge the different values based on the unique agent_id:
$agents = array();
foreach($agents_summary as $as){
$agents[$as['agent_id']]['first_name'] = $as['first_name'];
$agents[$as['agent_id']]['last_name'] = $as['last_name'];
if(empty($agents[$as['agent_id']][$as['transaction_type']])){
$agents[$as['agent_id']][$as['transaction_type']] = $as['transaction_count'];
}
else{
$agents[$as['agent_id']][$as['transaction_type']] += $as['transaction_count'];
}
}
foreach($agents as $agentID => $agent){
<tr>
<td><?=$agentID?></td>
<td><?=$agent['first_name']?></td>
<td><?=$agent['APPOINTMENT']?></td>
... etc...
</tr>
}
Upvotes: 1