so that as few SQL statements as possible, I want to make a choice from MySQL:
SELECT * FROM products WHERE category IN (10,120,150,500) ORDER BY category,id;
Now I have a list of products as follows:
CATEGORY
- product 1
- product 2
CATEGORY 2
- product 37
...
What is the best and most efficient way to handle MySQL?
I thought something like (pseudo PHP)
foreach ($product = fetch__assoc($result)){
$products[$category][] = $product;
}
and then, outputting it, execute the foreach loop:
foreach($categories as $category){
foreach($products[$category] as $product){
$output;
}
}
Is it the best, or something magical, like mysql_use_groupbyor something else?
source
share