0

This is my view. How can I calculate sum of rows using view without using model and controller

foreach($requests as $request):
<?php echo SUM($request->unit_price) ?>
<?php  endforeach; ?>
3

2 Answers 2

2
$sum = 0;
foreach ($requests as $request):
    $sum += $request->unit_price;
endforeach;
echo $sum;
Sign up to request clarification or add additional context in comments.

7 Comments

displays two values and its iterating
What do you mean by displays two values?
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
@Melebius : I will defiantly write that but this is so simple and short code.
oh sorry @urfusion i didnt identify you , my badness, good guy thanks
|
0
foreach($requests as $request){
$data[]=$request->unit_price;
}

now $data array contains all the unit prices.

echo array_sum($data);

and you get your answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.