本文介绍了显示来自多个 MySQL 表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示数据库中的数据时遇到了一个小问题...

我有两个数据库表:

  • 类别(cat_id,cat_name)
  • menu_items(menu_id, menu_name, cat_id, menu_description, menu_price)

我想将数据显示为:

类别 1 名称

  • cat_id = 1, menu_name, menu_description, menu_price
  • cat_id = 1, menu_name, menu_description, menu_price
  • cat_id = 1, ...

类别 2 名称

  • cat_id = 2、menu_name、menu_description、menu_price
  • cat_id = 2、menu_name、menu_description、menu_price
  • cat_id = 2, ...

类别 3 名称

  • cat_id = 3、menu_name、menu_description、menu_price
  • cat_id = 3、menu_name、menu_description、menu_price
  • cat_id = 3, ....

...

我的代码:

function get_menu_items() {//查询数据库以获取类别列表$conn = db_connect();$query = "从类别中选择 cat_id, cat_name";$result = @$conn->query($query);如果(!$结果){返回假;}$num_cats = @$result->num_rows;如果($num_cats == 0){返回假;}$result = db_result_to_array($result);返回 $result;}功能 display_menu_items($menu_array) {如果(!is_array($menu_array)){echo "

当前没有可用的菜单项

";返回;}foreach ($menu_array 作为 $row) {echo "<section id='".$row['cat_name']."'>";echo "

";echo "<h3>".$row['cat_name']."</h3>";***/* 问题 */***echo "
    ";foreach... {echo "<li>cat_id = #, menu_name, menu_description, menu_price</li>";}回声</ul>***/* 问题 */***echo "</div>";echo "</section>";}}

有什么建议吗?

谢谢.

解决方案
catRecordset = select * from CategoriescatRecordset 中的 foreach 猫{打印(猫名);menuRecordset = select * from menu where menyu.cat_id = cat.cat_idforeach( menuRecordSet 中的 menuItem ){打印(菜单项.无论什么);}}

I'm having a little problem displaying data from a database...

I have two database tables:

  • categories(cat_id, cat_name)
  • menu_items(menu_id, menu_name, cat_id, menu_description, menu_price)

I would like to display data as:

Categorie 1 Name

  • cat_id = 1, menu_name, menu_description, menu_price
  • cat_id = 1, menu_name, menu_description, menu_price
  • cat_id = 1, ...

Categorie 2 Name

  • cat_id = 2, menu_name, menu_description, menu_price
  • cat_id = 2, menu_name, menu_description, menu_price
  • cat_id = 2, ...

Categorie 3 Name

  • cat_id = 3, menu_name, menu_description, menu_price
  • cat_id = 3, menu_name, menu_description, menu_price
  • cat_id = 3, ....

...


My code:

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>";
  }
}


Any suggestions?

Thanks.

解决方案
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 );
    }
}

这篇关于显示来自多个 MySQL 表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 10:29