我在显示数据库中的数据时遇到了一些问题...
我有两个数据库表:
我想将数据显示为:
分类1名称
分类2名称
分类3名称
...
我的代码:
function get_menu_items() {
// query database for a list of categories
$conn = db_connect();
$query = "select cat_id, cat_name from categories";
$result = @$conn->query($query);
if (!$result) {
return false;
}
$num_cats = @$result->num_rows;
if ($num_cats == 0) {
return false;
}
$result = db_result_to_array($result);
return $result;
}
function display_menu_items($menu_array) {
if (!is_array($menu_array)) {
echo "<p>No menu items currently available</p>";
return;
}
foreach ($menu_array as $row) {
echo "<section id='".$row['cat_name']."'>";
echo "<div class='group course'>";
echo "<h3>".$row['cat_name']."</h3>";
***/* Problem */***
echo "<ul>";
foreach... {
echo "<li>cat_id = #, menu_name, menu_description, menu_price</li>";
}
echo "</ul>
***/* Problem */***
echo "</div>";
echo "</section>";
}
}
有什么建议吗?
感谢。
答案 0 :(得分:2)
catRecordset = select * from categories
foreach cat in catRecordset
{
print( cat.name );
menuRecordset = select * from menu where menyu.cat_id = cat.cat_id
foreach( menuItem in menuRecordSet )
{
print( menuItem.whatever );
}
}
答案 1 :(得分:0)
甚至更好地使用join并只运行一个查询
$Recordset = 'select * from categories cat LEFT JOIN menu ON menu.cat_id = cat.cat_id';
$category = '';
foreach $record in $Recordset
{
if ($category != $record['cat_id']) {
print( cat.name );
$category = $record['cat_id'];
}
print( $record['menu_name'] ..... );
}