我有客户,代理商和余额表。 我想按其所属代理名称显示数据。
例如-在下面的客户表中,我具有agentId和prodname,每个代理具有多个产品,例如abc和def属于代理raj,而xyz prod属于代理test,因此raj和test将显示一次。
平衡表的bal_amt与agent_id相关联,因此bal_amt将显示在每个agentname的页脚上。
Customertable
id agent_id catname
1 1 abc
2 1 def
3 2 xyz
Agenttable
id name
1 raj
2 test
Balancetable
id agent_id bal_amt
1 1 25000
2 2 7000
我的SQL查询数据
Array
(
[id] => 73
[agent_id] => 5
[prodname] => abc
[subprodname] => test
[quantity] => 2
[rate] => 120
[amount] => 240
[paid_amount] => 240
[balance] => Array
(
[agent_id] => 5
[bal_amt] => 14000
)
[agent] => Array
(
[id] => 5
[name] => Duryodhan nimbarte
)
)
Array
(
[id] => 72
[agent_id] => 5
[prodname] => abc
[subprodname] => test
[quantity] => 5
[rate] => 200
[amount] => 1000
[paid_amount] => 700
[balance_amount] => 300
[balance] => Array
(
[agent_id] => 5
[bal_amt] => 14000
)
[agent] => Array
(
[id] => 5
[name] => Duryodhan nimbarte
)
)
Array
(
[id] => 71
[agent_id] => 6
[prodname] => abc
[subprodname] => test
[quantity] => 3
[rate] => 200
[amount] => 600
[paid_amount] => 200
[balance_amount] => 400
[is_deleted] => 0
[balance] => Array
(
[agent_id] => 6
[bal_amt] => 14000
)
[agent] => Array
(
[id] => 6
[name] => Narhari Hedau
)
)
预期结果
Agentname catname subprodname quantity rate amount paid_amount
raj
abc test 10 5 100 80
def def 25 5 300 200
25000
test
xyz cde 35 10 1800 700
7000
下面是我的代码
<?php
$finalArray = [];
foreach ($customerlist as $user) {
$finalArray[$user['agent']['name']][] = $user;
}
foreach ($finalArray as $key => $value) {
?>
<tr>
<td colspan="3"><?php echo $key; ?></td>
</tr>
<?php foreach ($value as $user) { ?>
<tr>
<td> </td>
<td><?php if(!empty($user['product']['name'])){echo $user['product']['name']; } ?></td>
</tr>
<?php } } ?>
现在我正在低于结果
Agentname catname
raj
abc
def
test
xyz
答案 0 :(得分:1)
尝试此代码。
<?php
$customerlist = [ ['id' => 1, 'agent_id' => 1, 'prodname' => 'abc', "subprodname" => "test", "quantity" => 2, "rate" => 120, "amount" => 240,
"paid_amount" => 240, 'balance' => [ 'agent_id' => 1, 'bal_amt'=> 25000], 'agent' => ['name' => "raj"] ],
['id' => 2, 'agent_id' => 1, 'prodname' => 'def', "subprodname" => "test", "quantity" => 5, "rate" => 200, "amount" => 1000,
"paid_amount" => 700, 'balance' => [ 'agent_id' => 1, 'bal_amt'=> 25000], 'agent' => ['name' => "raj"] ],
['id' => 3, 'agent_id' => 2, 'prodname' => 'xyz', "subprodname" => "test", "quantity" => 3, "rate" => 200, "amount" => 600,
"paid_amount" => 200, 'balance' => [ 'agent_id' => 2, 'bal_amt'=> 7000], 'agent' => ['name' => "test"] ]
];
$finalArray = [];
foreach ($customerlist as $user) {
$finalArray[$user['agent']['name']]['x']['prodname'] = $user['balance']['bal_amt'];
$finalArray[$user['agent']['name']]['x']['agent'] = $user['agent']['name'];
}
foreach ($customerlist as $user) {
$finalArray[$user['agent']['name']][] = $user;
}
foreach ($finalArray as $key => $value) {
?>
<table border= "1px solid black">
<?php
foreach ( $value as $user) { ?>
<tr>
<td><?php if(!is_array($user['agent'])){ echo $user['agent'];} ?></td>
<td><?php echo $user['prodname']; /*if(!empty($user['product']['name'])){ }*/ ?></td>
<td><?php if(!empty($user['subprodname'])){ echo $user['subprodname'];} ?></td>
<td><?php if(!empty($user['quantity'])){ echo $user['quantity']; } ?></td>
<td><?php if(!empty($user['rate'])){ echo $user['rate']; } ?></td>
<td><?php if(!empty($user['amount'])){ echo $user['amount']; } ?></td>
<td><?php if(!empty($user['paid_amount'])){ echo $user['paid_amount']; } ?></td>
</tr>
<?php } } ?>
</table>
输出:
Agentname catname subprodname quantity rate amount paid_amount
raj 25000
abc test 10 5 100 80
def def 25 5 300 200
test 7000
xyz cde 35 10 1800 700