I need to find district combinations in a table, however, I also need to know how many times each of those occurred.
that is, I only want to display them once, but I do also need to know how many times those combinations exists.
this is for things that need to be ordered, and this particular part of the application should generate a list of what has to be ordered, but preferably in the same way you’d normally place the order.
so the list could be like this:
right now I have this:
$orders = OrderProducts::find()->select(['productId','sauceId'])->distinct()->all();
if($orders != null){
foreach($orders as $item){
echo $item->product->name;
if($item->sauce->id != 0){
echo ' '.$item->sauce->name;
}
echo '<br>';
}
}else{
echo 'nothing to order.';
}
but this lacks the amount of items, so in the example I gave above this would just say I need item 2 with mayo, but not that I need three of them.
Ideally I’d like to include the count of combinations in the same query, but so far I can’t get the the result at all, I did find examples that use count(), which makes sense since i do want to count something, only those just return how many combinations exists, not how many entries they represent.
so entries like this:
$counter = OrderProducts::find()->groupBy(['productId','sauceId'])->count();
but that only returns 4 (the example above it would return 5), but that is still how many groups exists, meaning I still don’t know how many of each things we need.
I though this would be pretty simple, but so far I can only find other examples that do the same as the code above, but they don’t count how many times each group exists in the database.