0

this is my session array of cart application in laravel 6

[cartS] => Array ( 

[0] => Array ( 
    [pid] => 3 
    [pname] => Watch 
    [price] => 500
    [qnty] => 5 
    [pimg] => 3.jpg 
    ) 
[1] => Array ( 
    [pid] => 1 
    [pname] => Mobile case
    [price] => 200 
    [qnty] => 3 
    [pimg] => 1.jpg 
    ) 
) 

i have a table in checkout page i am doing foreach loop so i can access $key => $value of session array

product    quantity   total_price
-------    --------   ------------
item1      2           100 ( here 100 is multipication of price and qunatity)
item2      4           200
item3      2           200
-------------------    ------------
total amount           500 

-----------i want to display 500 as a total amount----------

here is foreach loop in my checkout.blade.php file


<?php  $items = Session::has('cartS') ? Session::get('cartS') : null; ?>

@foreach($items as $key=>$value)
    <tr>
       <th class="w-50  font-size-14">
          {{ucfirst($value['pname'])}}
       </th>
       <td class="text-right font-size-14">
          {{$value['qnty']}}
       </td>
       <th class="text-right font-size-14">
           {{ $value['qnty'] * $value['price'] }}
       </th>
    </tr>
@endforeach
<h1>your total amount -----------</h1>

Am i doing somthing wrong or i have to display total amount in foreach loop?

1 Answer 1

2

you just need to sum the quantity multiplied by the price of each item:

<?php $items = Session::has('cartS') ? Session::get('cartS') : null; ?> 
@php $totalPrice = 0 @endphp 
@foreach($items as $key=>$value)
<tr>
  <th class="w-50  font-size-14">
    {{ucfirst($value['pname'])}}
  </th>
  <td class="text-right font-size-14">
    {{$value['qnty']}}
  </td>
  <th class="text-right font-size-14">
    {{ $value['qnty'] * $value['price'] }}
   @php 
    $totalPrice += $value['qnty'] * $value['price']; 
   @endphp
  </th>
</tr>
@endforeach
<h1>your total amount {{$totalPrice}}</h1>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.