Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中按组发布mysql数据_Php_Html_Mysql - Fatal编程技术网

如何在php中按组发布mysql数据

如何在php中按组发布mysql数据,php,html,mysql,Php,Html,Mysql,我在构建查询并在PHP中显示结果时遇到问题,结果应该在PHP中分组 给定以下示例数据集: +-----------+---------+ | category | item | +-----------+---------+ | fruit | apple | | fruit | apricot | | fruit | banana | | fruit | plum | | vegetable | carrot | | vegetable |

我在构建查询并在PHP中显示结果时遇到问题,结果应该在PHP中分组

给定以下示例数据集:

+-----------+---------+
| category  | item    |
+-----------+---------+
| fruit     | apple   |
| fruit     | apricot |
| fruit     | banana  |
| fruit     | plum    |
| vegetable | carrot  |
| vegetable | onion   |
| vegetable | potato  |
| vegetable | spinach |
+-----------+---------+
和PHP/MySQL代码:

<?php

    include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/SQLTutorial.db.inc.php';

    echo "<p>Here is a test:</p>";

    try {
    $sql = "select category, item from Foods";
      $result = $pdo->query($sql);
    }
    
    catch (PDOException $e) {
      $error = 'Error fetching data: ' . $e->getMessage();
      include 'error.html.php';
      exit();
    }

    foreach ($result as $row)
    {
      $ShoppingItems[] = array(
        'category' => $row['category'],
        'item' => $row['item']
      );
    }

    echo "<p>Here are the items by group:</p>";
        
    foreach ($ShoppingItems as $ShoppingItem)
    {
        echo "<p><b>" . $ShoppingItem['category'] . "</b>" 
                . $ShoppingItem['item'] . "</p>";            
    }

?>

我应该如何编写查询和php,以便在单个标记中格式化类别,如
,并且项目后面有不同的标记,如
  • ,因此结果如下所示

    水果
    • 苹果
    • 香蕉
    • 李子
    蔬菜
    • 胡萝卜
    • 洋葱
    • 马铃薯
    • 菠菜
    会议上的回答非常有帮助。下面的代码工作得很好——尽管我确信有一个更优雅的解决方案

    <?php
    
    include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/SQLTutorial.db.inc.php';
    
    echo "<p>Here is a test:</p>";
    
    try {
        $sql = "select * from Foods order by category, item";
        $result = $pdo->query($sql);
    }
    catch (PDOException $e) {
        $error = 'Error fetching data: ' . $e->getMessage();
        include 'error.html.php';
        exit();
    }
    
    // Loop through results and build the data into a data structure
    foreach($result as $row){
        $category = $row['category'];
        $item = $row['item'];
        if (!isset($list[$category])) {// if this is this a category we've never seen before
            $list[$category] = array($item);//then add it to the list with a new array to hold its items.
        } else {
            $list[$category][] = $item;      //otherwise just push the new item onto the category's array of items.
        }    
    }
    
    /*        
    print "<pre>\n";
    print_r($list);  // <-- a very useful debugging tool
    print "</pre>\n";
    */            
    
    // And now to print out the formatted results.
    foreach ($list as $category => $items) {
        echo "<h1>" . htmlspecialchars($category, ENT_COMPAT, "utf-8") . "</h1><ul>\n";
        foreach ($items as $item) {
            echo "<li>" . htmlspecialchars($item, ENT_COMPAT, "utf-8") . "</li>\n";
        }
        echo "</ul>\n";
    }
    
    ?>
    
    
    
    向我们展示您迄今为止编写的代码。来吧。任何关于PHP和MySQL的基础教程都会告诉您如何做到这一点。不够好。这个问题似乎离题了,因为它显示了负面的研究成果。我添加了示例代码,以便更好地寻求您的帮助。再次感谢。