I am trying to do a restaurant site where the people can see the menu.
I have a table that looks like this:

And now I want to output this data according the title:
Pizza
Ham & Cheese $150
Onion & Cheese $120
Salad
Caesar $70
Tomate & Olives $60
Dessert
Icecream $110
Vanilla Cake $90
-
Well at the future the menu_title can be changed by the client... That means the title need to be retrieved also from the database.
Here is the code I am trying but I don't have idea how to add the content below the title:
<?PHP
$sql_product="SELECT * FROM cc_restaurants_menu WHERE menu_asoc='$asoc' AND menu_type='product' GROUP BY menu_title";
$stmt_product = $conn->prepare($sql_product);
$stmt_product->execute();
$result_product = $stmt_product->setFetchMode(PDO::FETCH_ASSOC);
if($result_product > 0) {
while($row = $stmt_product->fetch()) {
echo '<h3>'. $row['menu_title'] .'</h3><br><p>'. $row['menu_product'] .'</p>';
}
}
?>
But this code only output the title and the first row :S
Any idea?
EDIT
I got 2 answer correct:
OPTION 1
$sql_product="SELECT * FROM cc_restaurants_menu WHERE menu_asoc='$asoc' AND menu_type='product'";
$stmt_product = $conn->prepare($sql_product);
$stmt_product->execute();
$result_product = $stmt_product->setFetchMode(PDO::FETCH_ASSOC);
if($result_product > 0) {
while($row = $stmt_product->fetch()) {
$menuArr[$row['menu_title']][] = '<p>'. $row['menu_product'] . ''. $row['menu_product_price'] . ''. $row['menu_product_desc'] .'</p>';
}
foreach($menuArr as $menuTitle => $productArr){
echo '<h3>'. $menuTitle .'</h3></br>';
foreach($productArr as $key =>$productname){
echo '<p>'. $productname .'</p>';
}
}
}
OPTION 2
$sql_product="SELECT * FROM cc_restaurants_menu WHERE menu_asoc='$asoc' AND menu_type='product' ORDER BY menu_title";
$stmt_product = $conn->prepare($sql_product);
$stmt_product->execute();
$result_product = $stmt_product->setFetchMode(PDO::FETCH_ASSOC);
$title = "";
while ($row = $stmt_product->fetch()) {
if ($row['menu_title'] != $title) {
echo '<h3>'.$row['menu_title'].'</h3><br>';
$title = $row['menu_title'];
}
echo '<p>'.$row['menu_product'].'</p><p>'.$row['menu_product_price'].'</p>';
}
Please try this
$sql_product="SELECT * FROM cc_restaurants_menu WHERE menu_asoc='$asoc' AND menu_type='product'";
$stmt_product = $conn->prepare($sql_product);
$stmt_product->execute();
$result_product = $stmt_product->setFetchMode(PDO::FETCH_ASSOC);
if($result_product > 0) {
while($row = $stmt_product->fetch()) {
$menuArr[$row['menu_title']][] = $row['menu_product'] . " ".$row['menu_price'];
}
foreach($menuArr as $menuTitle => $productArr){
echo '<h3>'. $menuTitle .'</h3>';
foreach($productArr as $key =>$productname){
echo '<p>'. $productname .'</p>';
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With