2

我想从 MySQL 数据库中获取今天的销售数据。使用下面的代码,我只能在图表中显示一个菜单 ID。我想展示它,就像有 S001、S002、S003、S004 和 S005。即使是零,也会显示项目的数量。

function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Food');
    data.addColumn('number', 'Quantity');
    data.addRows([
      <?php
        $i = 0;
        require "init.php";
        $menu_id = "S001";

        //query db
        $sql = "SELECT m.food_name, SUM(o.quantity) AS quantity FROM `Order` o INNER JOIN `Menu` m ON m.menu_id = o.FK_menu_id WHERE DATE(`ordered_on`) = CURDATE() AND o.FK_menu_id = '$menu_id'";
        $result = mysqli_query($con, $sql) or die("Error: ".mysqli_error($con));

        while ($row = mysqli_fetch_array($result)) {
          $i++;
          echo "['".$row['food_name']."', ".$row['quantity']."]";
          if ($i < $row) {
            echo ",";
          }
        }
      ?>
    ]);

    // Set chart options
    var options = {'title':"Today's Sales",
                   'width':600,
                   'height':450,
                   'legend':'none'};

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }

该图是当前代码的输出: 在此处输入图像描述

4

0 回答 0