mysql - PHP Group sql query under the same title -
i trying restaurant site people can see menu.
i have table looks this:
and want output data according title:
pizza
ham & cheese $150
onion & cheese $120
salad
caesar $70
tomate & olives $60
dessert
icecream $110
vanilla cake $90
-
well @ future menu_title can changed client... means title need retrieved database.
here code trying don't have idea how add content below title:
<?php $sql_product="select * cc_restaurants_menu menu_asoc='$asoc' , menu_type='product' group 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 code output title , first row :s
any idea?
edit
i got 2 answer correct:
option 1
$sql_product="select * cc_restaurants_menu menu_asoc='$asoc' , 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 $menutitle => $productarr){ echo '<h3>'. $menutitle .'</h3></br>'; foreach($productarr $key =>$productname){ echo '<p>'. $productname .'</p>'; } } }
option 2
$sql_product="select * cc_restaurants_menu menu_asoc='$asoc' , menu_type='product' order 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 * cc_restaurants_menu menu_asoc='$asoc' , 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 $menutitle => $productarr){ echo '<h3>'. $menutitle .'</h3>'; foreach($productarr $key =>$productname){ echo '<p>'. $productname .'</p>'; } } }
Comments
Post a Comment