I have a table like this
table name: products
+----+-------------+-----------+
| id | name | type |
| 1 | apple | fruit |
| 2 | banana | fruit |
| 3 | tomato | vegetable |
| 4 | egg plant | vegetable |
| 5 | carrot | vegetable |
| 6 | Minced Steak| meat |
| 7 | Pork ribs | meat |
+----+-------------+-----------+
I want output html like:
<ul>
<li>apple</li>
<li>banana</li>
</ul>
<ul>
<li>tomato</li>
<li>egg plant</li>
<li>carrot</li>
</ul>
<ul>
<li>Minced Steak</li>
<li>Pork ribs</li>
</ul>
I do not know whether there have a easy mysql query. my complex mysql query as below:
$result = mysql_query("SELECT * FROM products group by type ORDER BY type");
while ($data = mysql_fetch_array($result)){
echo '<ul>';
$querys = "select * FROM products WHERE type = ".$data['type']." ";
$results = mysql_query($querys);
while($row = mysql_fetch_array($results)){
echo '<li>'.$row['name'].'</li>';
}
echo '</ul>';
}
Obviously this is inefficient, how to improve?
You only need the first query.
Now the
$grouped_datagives the result array you will like.For outputting, it is also simple: