Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.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
HTML表Php中显示的SQL数据_Php_Mysqli - Fatal编程技术网

HTML表Php中显示的SQL数据

HTML表Php中显示的SQL数据,php,mysqli,Php,Mysqli,我从MYSQL表中提取了以下格式的数据 美国广播公司2016 567 ABC 2017 456 ABC 2018 987 DEF 2016 537 DEF 2017 452 EFG 2016 537 EFG 2017 452 EFG 2018 687 我需要通过Php以以下格式在HTML表格中显示它, 现场2016 2017 2018 ABC 567456987 DEF537452 EFG 537 452 687 任何帮助都将不胜感激 SQL查询: Php代码: <?php $

我从MYSQL表中提取了以下格式的数据

美国广播公司2016 567 ABC 2017 456 ABC 2018 987 DEF 2016 537 DEF 2017 452 EFG 2016 537 EFG 2017 452 EFG 2018 687

我需要通过Php以以下格式在HTML表格中显示它, 现场2016 2017 2018 ABC 567456987 DEF537452 EFG 537 452 687

任何帮助都将不胜感激

SQL查询:

Php代码:

    <?php
  $sql = "SELECT sum(Volume) as Vol from rad_vol WHERE Modality like '%MR' group by Site, Year ";
  echo $sql;
  $result = mysqli_query($conn, $sql);
  $sql = "SELECT DISTINCT Year from rad_vol";
  $res_year = mysqli_query($conn, $sql);
  $sql = "SELECT DISTINCT Site from rad_vol";
  $res_site = mysqli_query($conn, $sql);
?>
<div id="content">
  <article>
    <table>
      <thead>
        <tr>
          <th>
            SITE
          </th>
          <?php
            if (mysqli_num_rows($res_year) > 0){
              $yr_count=0;
              while ($rows = mysqli_fetch_array($res_year))
              {
                $year = $rows["Year"];
                echo '<th>' . $year . '</th>';
                $yr_count++;
              }
              echo $yr_count;
            }
          ?>
        </tr>
        <?php
          while ($rows = mysqli_fetch_array($res_site))
          {
            echo '<tr><th>' . $rows["Site"] . '</th>';
            $i = 0;
            while($vols = mysqli_fetch_array($result))
            {
              echo '<td>' . $vols["Vol"] . '</td>';
              $i++;
              if ($i % 4 == 0){
                echo '</tr>';
                break;
              }
            }
          }
        ?>

      </thead>
    </table>

你离这里不远

如果将一个查询映射到多个数组并使用它们,则可以使用单个查询完成此操作

SQL:

这将为您提供如下记录集:

Site    Year    Vol
ABC     2016    567
ABC     2017    456
ABC     2018    987
DEF     2016    537
DEF     2017    452
EFG     2016    537
EFG     2017    452
EFG     2018    687
Array
(
    [ABC] => Array
        (
            [2016] => 567
            [2017] => 456
            [2018] => 987
        )

    [DEF] => Array
        (
            [2016] => 537
            [2017] => 452
        )

    [EFG] => Array
        (
            [2016] => 537
            [2017] => 452
            [2018] => 687
        )
)
然后,您可以循环遍历该记录集,并将其推送到几个数组中:

$a = []; // this will be your data
$b = []; // this will be a list of years

if($response = mysqli_query($db, $sql)) {
    while($row = mysqli_fetch_assoc($response)) {

        // set the site to a var as we're going to use it
        // a couple of times
        $site = $row['Site'];

        // push the site onto the array as an index
        if(!array_key_exists($site, $a)) $a[$site] = [];

        // add the year and volumne to the site
        $a[$site][$row['Year']] = $row['Vol'];

        // add the year to the list if it's not already in
        if(!in_array($row['Year'], $b)) $b[] = $row['Year'];
    }
}
$a将保存您重组后的数据,而$b只是一个年份列表$a应该是这样的:

Site    Year    Vol
ABC     2016    567
ABC     2017    456
ABC     2018    987
DEF     2016    537
DEF     2017    452
EFG     2016    537
EFG     2017    452
EFG     2018    687
Array
(
    [ABC] => Array
        (
            [2016] => 567
            [2017] => 456
            [2018] => 987
        )

    [DEF] => Array
        (
            [2016] => 537
            [2017] => 452
        )

    [EFG] => Array
        (
            [2016] => 537
            [2017] => 452
            [2018] => 687
        )
)
如果你歪着头眯着眼睛,数据的结构就更像你想如何使用它了。现在,您只需要循环输出:

<div id="content">
    <article>
        <table>
            <thead>
                <tr>
                    <th>Site</th>
                    <!-- loop through the years -->
                    <?php foreach($b as $y): ?>
                    <th><?= $y; ?></th>
                    <?php endforeach; ?>
                </tr>
            </thead>
            <tbody>
                <!-- loop through the sites -->
                <?php foreach($a as $siteName => $siteData): ?>
                <tr>
                    <!-- echo out the site name -->
                    <td><?= $siteName; ?></td>
                    <!-- 
                    loop through the years again so that the columns
                    are generated in the same way as they were for
                    the <thead> row
                    -->
                    <?php foreach($b as $y): ?>
                    <!--
                    check to see if *this* site has data for the current
                    year in the loop - if so echo that out in a <td>
                    otherwise just echo an empty <td>
                    -->
                    <td><?= array_key_exists($y, $siteData) ? $siteData[$y] : " "; ?></td>
                    <?php endforeach; ?>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </article>
</div>
全套装备

这只是整个脚本的转储,就像它在我的开发框中一样,样式和注释都被删除了

<?php
header('Content-Type: text/html;charset=utf-8');
$db = mysqli_connect('127.0.0.1', '******', '******', 'test');

$sql = <<<SQL
SELECT 
v.Site,
v.Year,
SUM(Volume) AS Vol 

FROM rad_vol AS v 

WHERE v.Modality LIKE '%MR' 

GROUP BY v.Site, v.Year 
SQL;

$a = [];
$b = [];

if($response = mysqli_query($db, $sql)) {
    while($row = mysqli_fetch_assoc($response)) {

        $site = $row['Site'];
        if(!array_key_exists($site, $a)) $a[$site] = [];

        $a[$site][$row['Year']] = $row['Vol'];
        if(!in_array($row['Year'], $b)) $b[] = $row['Year'];
    }
}

?><!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Test</title>
    </head>
    <body>
        <div id="content">
            <article>
                <table>
                    <thead>
                        <tr>
                            <th>Site</th>
                            <?php foreach($b as $y): ?>
                            <th><?= $y; ?></th>
                            <?php endforeach; ?>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($a as $siteName => $siteData): ?>
                        <tr>
                            <td><?= $siteName; ?></td>
                            <?php foreach($b as $y): ?>
                            <td><?= array_key_exists($y, $siteData) ? $siteData[$y] : " "; ?></td>
                            <?php endforeach; ?>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </article>
        </div>
    </body>
</html>

为什么不把查询做得更好,让结果如你所愿地显示出来,然后谷歌如何在php中回显HTML。>insert hi and welcome to stack overflow请阅读这里的gumph,你应该重写查询。Mysql可以为您分组这些数据。您好,欢迎使用SO。如果您已经为此编写了代码,但无法开始工作,那么您就来对了地方。只需编辑您的问题并将代码的相关部分添加到其中。您需要展示自己的努力,因为堆栈溢出不是“为我编写代码”服务。请看。我已经用我正在使用的代码更新了我的问题。非常感谢您在这方面的帮助。这正是我所要找的,只做了几处小改动,代码工作得非常好,并且按照要求显示了数据。现在我正在尝试理解数组,并尝试添加一个带有moris.js的图。谢谢