I have data like this sorted by Category:
Category 1, Product 1, Amount
Category 1, Product 2, Amount
Category 2, Product 3, Amount
I like to print it using blade as
Category 1
Product 1 Amount
Product 2 Amount
....
Total Amount of Category 1 : xxxxx
Category 2
Product 3 Amount
....
Total Amount of Category 2 : xxxxx
I am assuming that $categories is the sorted multidimensional array with indexes category,product and amount.
#for ($i = 0; $i < count($categories) ; $i++)
<?php $category_product_toatl=0; ?>
{{ $categories[$i]['category'] }}
{{ $categories[$i]['product'] }} {{ $categories[$i]['amount']}}
<?php
$category_product_total= $category_product_total+ $categories[$i]['amount'];
?>
#for ($j = $i+1; $j < count($categories) ; $j++)
#if($categories[$i]['category']== $categories[$j]['category'])
{{ $categories[$j]['product'] }} {{ $categories[$j]['amount']}}
<?php
$category_product_total= $category_product_total+ $categories[$j]['amount'];
?>
#else
break;
#endif
#endfor
Total Amount of {{ $category[$i] }} : {{ $category_product_total }}
#endfor
Related
Inside a foreach loop of products, I want to display each of the elements in unequal/uneven column sizes that make up a total of 12 just like what we have in the screenshot below. How can I achieve this in Laravel?
You can assign the col to each product in controller:
$cols = [3, 9, 3, 6, 3, 6, 6];
foreach($products as $key => $product){
$product->col = $cols[$key % count($cols)];
}
Blade:
#foreach($products as $product)
<div class="col-{{ $product->col }}"></div>
#endforeach
I do a select and get several results in array but I need to get the correct value for each step and set up a condition.
$step = DB::table('records')->where('id_user',$userId)->get();
for($i = 0; $i < count($step); $i++)
{
echo $step[$i]->id_step;
}
Id_step returns me values for each step where on the blade I need to get and see if id_step = 1 is true id_step = 2 is true.
This for is returning me only one value and it has 3 records in the table.
First of all. After a select you get an instance of Eloquent\Collection
Not an array.
So that said to loop do this:
$steps = DB::table('records')->where('id_user',$userId)->get();
foreach($steps as $row) {
echo $row;
}
Since you are familiar with arrays do this:
$steps->toArray();
Now your result is an array
Working insert in view this code.
#for($i = 0; $i < count($step); $i++)
#if($step[$i]->id_step == 2)
working
#else
not working
#endif
#endfor
Trying to write the cleanest code in Blade of a foreach where in the position 3 (if first and second exists) add a different code.
I'm trying using chunk() but it doesnt allow do non regulars array.
Write position 1 if exists
Write position 2 if exists with different class
Write some content between array position 2 and 3 from different array
Write position 3 if exists
Write position 4 if exists with different class
...
why u use chunk? u can use $key
#foreach($arr as $key => $value)
#if ($key === 2)
do something
#endif
#endforeach
or
#for ($i = 0; $i < count($arr); $i++)
#if ($i === 2)
do something
#endif
#endfor
How can i get sum of all these counts?
{{ DB::table('A')->count()}}
{{ DB::table('B')->count()}}
{{ DB::table('C')->count()}}
{{ DB::table('D')->count()}}
// Your Tables Which You Know Them Counts
$tables = ['A', 'B'];
// Our Count Start From Zero
$count = 0;
// Count Each Table And Add Our Count
foreach ($tables as $table) {
$tableCount = DB::table($table)->count();
$count += $tableCount;
}
// Display Count With dd()
dd( $count );
// Or Display In View
return view('index', compact('count'));
Like this?:
{{ $a = DB::table('A')->count()}}
{{ $b = DB::table('B')->count()}}
{{ $c = DB::table('C')->count()}}
{{ $d = DB::table('D')->count()}}
{{ $sum = $a + $b + $c + $d }}
But you may want to send all these data from a controller method instead of doing it in a view.
I'm new in magento.
I'm wondering how I can count all products using stock quantity. For example, I have
category 1
product one - stock 10
product two - stock 5
category 2
product three - stock 10
The result of the sum of all products should be 25
Actually, I'm using
<?php
$prods = Mage::getModel('catalog/product')->getCollection();
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($prods);
$count = number_format(count($prods));
echo $count;
?>
but this counts the products without stock quantity.
Thanks for your help.
Untested but this should get you what you need…
$stockItemCollection = Mage::getModel('cataloginventory/stock_item')
->getCollection();
$stockTotal = array_sum($stockItemCollection->getColumnValues('qty'));
This should work, too. The reports collection joins together all the quote_items. But I'm not sure wether any order status is considered
$collection = Mage::getResourceModel('reports/product_sold_collection');
$collection->addOrderedQty();
// EDIT reading the question is all
$sum = 0;
foreach($collection as $product) {
$sum += $product->getOrderedQty();
}
echo $sum;