首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在forloop php中按照组名显示数据

在forloop php中按照组名显示数据
EN

Stack Overflow用户
提问于 2018-07-07 13:24:14
回答 1查看 60关注 0票数 0

我有客户表、代理表和余额表。我想根据它们所属的代理名称来显示数据。

对于客户表下面的ex - In,我有agentId和prodname,每个代理都有多个产品,如abc和def属于代理raj,xyz prod属于代理test,所以raj和test将显示一次。

Balance表具有与其agent_id关联的bal_amt,因此bal_amt将显示在每个代理名称的页脚。

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

My sql query data

     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>&nbsp;</td> 
        <td><?php if(!empty($user['product']['name'])){echo $user['product']['name']; } ?></td>
    </tr>
<?php } } ?>

现在我的结果低于

Agentname  catname 
raj
            abc
            def
test
            xyz
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-07 14:50:39

试试这段代码。

<?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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51220170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档