如何为每一天的每个列添加值?

时间:2018-01-07 01:05:09

标签: php html mysqli sum

我有一个表输入所有必要的数据进行统计。我现在面临的问题是,我不知道如何为每个列添加值,但它只是为每天添加值。然后将其显示在一行或另一个表中。

此代码用于调出所有值并将其插入表格

<html>
<head>
<title>Your Home Page</title>
    </head><body>
<table border="1">
  <tr>
    <th>Date</th>
    <th>Participant ID</th>
    <th>Gender</th>
    <th>Hand</th>
    <th>Udnder 18</th>
    <th>Adult</th>
    <th>R</th>
    <th>C</th>
    <th>L</th>
    <th>Cash</th>
    <th>Card</th>
    <th>Ammount</th>
  </tr>
  <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "***";
    
    $mysqli = new mysqli($servername, $username, $password, $dbname);

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
} 
  $sql = "SELECT * FROM table ORDER BY Date ASC"; 
  $result = mysqli_query($mysqli, $sql) or die(mysqli_error());

  $datum = '';
  while($set = mysqli_fetch_assoc($result)) {
    ?>
      <tr>
        <td>
          <?php 
          if ($datum == $set["Date"]) {
            echo '&nbsp;';
          } else {
            echo $set["Date"];
          }
          ?>
        </td>
            <td><?php echo $set["ParticipantID"]; ?></td>
            <td><?php echo $set["Gender"]; ?></td>
            <td><?php echo $set["Hand"]; ?></td>
            <td><?php echo $set["Under18"]; ?></td>
            <td><?php echo $set["Adult"]; ?></td>
            <td><?php echo $set["r"]; ?></td>
            <td><?php echo $set["c"]; ?></td>
            <td><?php echo $set["l"]; ?></td>
            <td><?php echo $set["Cash"]; ?></td>
            <td><?php echo $set["Card"]; ?></td>
            <td><?php echo $set["Ammount"]; ?></td>
      </tr>
    <?php $datum = $set["Date"]; ?>
  <?php
  }
  ?>
</table> </body></html>

enter image description here

因此,对于colum R,04-01-2018总计值应为2.等。

感谢您的帮助。 亲切的问候 标记

1 个答案:

答案 0 :(得分:1)

解决此问题的第一种方法是使用第二个MySQL查询来获取每个日期的总值。之后创建一个函数printDateTableRow来打印表的每一行。使用此函数的mysqli_fetch_assoc($totalMysqliResult)参数,您可以传递一行的值,其中包含每个新日期的总值。

另一种方法是使用WITH ROLLUP组修饰符(您可以尝试自己编写此类查询)。

以下是第一种方式的工作代码:

<html>
<head>
    <title>Your Home Page</title>
</head>
<body>
<table border="1">
  <tr>
    <th>Date</th>
    <th>Participant ID</th>
    <th>Gender</th>
    <th>Hand</th>
    <th>Udnder 18</th>
    <th>Adult</th>
    <th>R</th>
    <th>C</th>
    <th>L</th>
    <th>Cash</th>
    <th>Card</th>
    <th>Ammount</th>
  </tr>
  <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "***";

    $mysqli = new mysqli($servername, $username, $password, $dbname);

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}
  $sql = "SELECT * FROM `table` ORDER BY Date ASC";
  $result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));


$totalMysqliResult = mysqli_query($mysqli, '
    SELECT
        Date, "" AS ParticipantID, "" AS Gender, "" AS Hand,
        SUM(Under18) AS date_total_Under18,
        SUM(Adult) AS date_total_Adult, SUM(r) AS date_total_r,
        SUM(c) AS date_total_с, SUM(l) AS date_total_l,
        SUM(Cash) AS date_total_Cash, SUM(Card) AS date_total_Card,
        SUM(Amount) AS date_total_Amount
    FROM `table`
    GROUP BY Date
    ORDER BY Date ASC') or die(mysqli_error($mysqli));

/**
* @param $row
* @param null $dateFieldHeader
*/
function printDateTableRow($row, $dateFieldHeader = null) {
  ?><tr><?
  foreach ($row as $key => $curVal) {
      ?><td><?
      if ($key == 'Date') {
          echo $dateFieldHeader == null ? $curVal : $dateFieldHeader;
      } else {
          echo $curVal;
      }
      ?></td><?
  }
  ?></tr><?
}

$previousDate = null;
$oneDateRowsCount = 0;

while($row = mysqli_fetch_assoc($result)) {
    if ($row['Date'] == $previousDate) {
        $oneDateRowsCount++;
        printDateTableRow($row, '&nbsp;');
    } else {

        if ($previousDate !== null) {
            printDateTableRow(mysqli_fetch_assoc($totalMysqliResult), 'Date total');
        }

        printDateTableRow($row);
        $oneDateRowsCount = 1;
    }

    $previousDate = $row['Date'];
}

// add total for the last rows with the same date
if ($oneDateRowsCount > 1) {
    printDateTableRow(mysqli_fetch_assoc($totalMysqliResult), 'Date total');
}
?>
</body>
</html>

您应该以OOP样式重构此代码,并使用PDO而不是mysqli。