Reputation: 319
I have this in my gridview:
[
'value' => function ($data) {
$summ = 0;
$str = 0;
foreach($data->itemsOrder as $request) {
$str .= $request->order_items_quantity;
$summ += $str;
}
return $summ;
},
],
I get something like that '123' in column cell, but not '6'. How Could I get sum of elements?
Upvotes: 0
Views: 532
Reputation: 3008
Because you are concatenating string and not adding number.
So replace $str .= $request->order_items_quantity;
with
$str += floatval($request->order_items_quantity);
or if order_items_quantity is an integer
$str += intval($request->order_items_quantity);
Upvotes: 1
Reputation: 710
gridview [
'value' => function ($data) {
$summ = 0;
$str = 0;
foreach($data->itemsOrder as $request) {
//$str .= $request->order_items_quantity; //this means you are concating string each time which you dont need to do
$str = $request->order_items_quantity;
$summ += $str;
}
return $summ;
},
],
Hope you get the meaning of .
in php
Upvotes: 1
Reputation: 7004
Try this..
gridview [
'value' => function ($data) {
$summ = 0;
$str = 0;
foreach($data->itemsOrder as $request) {
$summ +=$request->order_items_quantity;
}
return $summ;
},
],
Upvotes: 1