Aleksa
Aleksa

Reputation: 99

codeigniter foreach multiple entries with same ID, sum elements and displey as one

I use foreach to make table with data from "sale" row json_decode($row['sale']), that now show each entry separately, what I wanted to do is to show each entry with same id [product_id] as one and to sum [num], sum [product_pcs], sum [price_per_pcs] of all entries with same id and show it one row.

json_decode($row['sale']):

Array ( [0] => stdClass Object ( [stock_id] => 501 [product_id] => 7 [product_pcs] => 1120 [price_per_pcs] => 1.17 [num] => 1) [1] => stdClass Object ( [stock_id] => 500 [product_id] => 7 [product_pcs] => 1120 [price_per_pcs] => 1.17 [num] => 1 ) [2] => stdClass Object ( [stock_id] => 497 [product_id] => 3 [product_pcs] => 600 [price_per_pcs] => 1,1 [num] => 1 ) [3] => stdClass Object ( [stock_id] => 496 [product_id] => 3 [product_pcs] => 600 [price_per_pcs] => 1,1 [num] => 1 ) [4] => stdClass Object ( [stock_id] => 526 [product_id] => 25 [product_pcs] => 1120 [price_per_pcs] => 1.22 [num] => 1 ) [5] => stdClass Object ( [stock_id] => 525 [product_id] => 7 [product_pcs] => 1120 [price_per_pcs] => 1.17 [num] => 1 ) )

I display the received data on this way:

<table>
<thead>
                        <tr>
                            <th>No</th>
                            <th>Product ID</th>
                            <th>Price Per Piece</th>
                            <th>Product pcs</th>
                            <th>Total products</th>
                    <?php 
                        $count      =   1;
                        $sale   = json_decode($row['sale']);
                        foreach ($sale as $product):
                    ?>
                        </tr>
 </thead>
  <tbody> 
   <tr>
                            <td><?php echo $count++;?>.</td>
                            <td><?php echo $product->product_id; ?></td>
                            <td><?php echo $product->price_per_pcs;?></td>
                            <td><?php echo $product->product_pcs; ?></td>
                            <td><?php echo $product->num; ?></td>
                        </tr>
                    <?php endforeach;?>
</tbody> 
</table>

With the above I get a table that looks like this: With the above I get a table that looks like this:
And I want to look like this:

And I want to look like this:

I would be very grateful.

Upvotes: 0

Views: 1943

Answers (1)

Michael Krikorev
Michael Krikorev

Reputation: 2156

The best way would be to alter the original SQL-query, but since that information is lacking at the moment, you could create a new array with the sums you need with something like:

<table>
<thead>
    <tr>
        <th>No</th>
        <th>Product ID</th>
        <th>Price Per Piece</th>
        <th>Product pcs</th>
        <th>Total products</th>
    </tr>
 </thead>
 <tbody>

    <?php 
    $sale = json_decode($row['sale']);

    $sum = array(); // INITIATE NEW ARRAY WITH SUMS

    //  SUM UP VALUES IN A NEW ARRAY...
    foreach ($sale as $product){ 
        // CHECK IF ITEM IS SET TO PREVENT "Undefined offset" ERRORS... 
        if (!isset($sum[$product->product_id])) { 
            // INITIATE ARRAYS.. THE PRODUCT ID IS USED AS ARRAY KEY 
            $sum[$product->product_id]['price_per_pcs'] = $product->price_per_pcs;
            $sum[$product->product_id]['product_pcs'] = $product->product_pcs;
            $sum[$product->product_id]['product_num'] = $product->num;       
        } else { // ARRAYS ALREADY SET.. USE ASSIGNMENT OPERATORS TO SUM UP THE VALUES
            $sum[$product->product_id]['price_per_pcs'] += $product->price_per_pcs;
            $sum[$product->product_id]['product_pcs'] += $product->product_pcs;
            $sum[$product->product_id]['product_num'] += $product->num;
        } 
    }

    // LOOP THROUGH SUMS ARRAY...   
    $count = 1;
    foreach ($sum as $key=>$val){
    ?>
   <tr>
        <td><?php echo $count++;?>.</td>
        <td><?php echo $key; ?></td>
        <td><?php echo $val['price_per_pcs'];?></td>
        <td><?php echo $val['product_pcs']; ?></td>
        <td><?php echo $val['product_num']; ?></td>
    </tr>
<?php } ;?>
</tbody> 
</table>

Let me know if this works for you.. or if i can assist you with any other solution.

Upvotes: 1

Related Questions