PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] Display data as per their group name inside forloop php

 January 16, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

I havecustomer,agent and balance table. I want to display data as per their belonging agentname.

for ex - In below customer table,i have agentId and prodname,each agent have multiple products like abc and def belongs to agent raj and xyz prod belongs to agent test ,So raj and test will display once.

Balance table have bal_amt associated with thier agent_id,so bal_amt will display on footer of each 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

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
        )

)

Expected result

 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

Below is my code

<?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 } } ?>

Right now i am getting below result

Agentname  catname 
raj
            abc
            def
test
            xyz

Solution

Try this code.

<?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>

Output:

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


Answered By - Shivrudra
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing