I have a menu. Every meal has a category like beef, pork, chicken, salad. Now I want to fetch every meal, split them according to the category and save them in an array. So that I have for every category an Array with meals. The last step would be counting the amount of meals in each category but this is easy when I am there.
My current attempt is very bad. I make a mysql_query
with a filter for every single category and a second to count the datasets. So I have for one category two queries. If I have at the end like 30 categories, the server have to process 60 queries for one page view.
for($i=0; $i < $30; $i++)
{
$fetchMeal="SELECT * FROM Menu WHERE Categorie = '$categorie[$i]'";
$countMeals = mysql_query("SELECT count(*) from Menu WHERE Categorie = '$categorie[$i]'");
}
I want only one to fetch all datasets, split them in categories and count the datasets within these categories. Does anyone know a good and easy way to accomplish this?
Example table:
name | categorie
-----------------------
tomato-salad | salads
potatotfish | fish
peppersteak | beef
schnitzel | pork
ceasar-salad | salad
another-fish | fish
fish-fish | fish
roast pork | pork
etc...
now I want to have arrays for each category:
salads[0] -> tomato-salad
salads[1] -> ceasar-salas
fish[0] -> potatofish
fish[1] -> another-fish
fish[2] -> fish-fish
beef[0] -> peppersteak
pork[0] -> schnitzel
pork[1] -> roast pork