【问题标题】:Date record from MYSQL to display in a html table based from the dates Month来自 MYSQL 的日期记录显示在基于日期 Month 的 html 表中
【发布时间】:2019-12-26 05:37:06
【问题描述】:

我在 mysql 中有一个有 4 列的表。 2 个 VARCHAR 字段和 1 个日期字段,其中 NULL 字段为“是”,phpmyadmin 中的默认字段为“NULL”。我希望能够在具有 12 列的 html 表上显示每个月(例如 Jan、Feb 等)的 MYSQL 表中的日期 Month 到正确的月份列。例如,如果我的日期字段为 2019 年 12 月 8 日,我希望它显示在八月列上。

我尝试使用每个月的日期字段并使用带有 12 个日期选择器的表单,但我发现我不能有任何空的日期字段。

我不知道使用什么代码在月份列中显示日期月份,但这是 html

<table class="table table-bordered table-sm small">
  <thead class="thead-dark">
  <tr>
      <th>Owner</th>
      <th>Jan</th>
      <th>Feb</th>
      <th>Mar</th>
      <th>T1 Break</th>
      <th>Apr</th>
      <th>May</th>
      <th>Jun</th>
      <th>T2 Break</th>
      <th>Jul</th>
      <th>Aug</th>
      <th>Sep</th>
      <th>T3 Break</th>
      <th>Oct</th>
      <th>Nov</th>
      <th>Dec</th>
      <th>T4 Break</th>
  </thead>
  </tr>
</Table>

For example if my date field has 12/8/2019 i want that to be displayed on the August Column.

<?php
require 'db.php';
$sqlview="SELECT * FROM booking";
$myData = mysqli_query($con, $sqlview);
while($viewrecord = mysqli_fetch_array($myData))
  {
echo "<form action=overseebooking.php method=POST>";
      echo "<tr>";
      echo "<td>" . $viewrecord['booking_owner'] . " </td>";
      echo "<td>" . $viewrecord['booking_jan'] . " </td>";
      echo "<td>" . $viewrecord['booking_feb'] . " </td>";
      echo "<td>" . $viewrecord['booking_march'] . " </td>";
      echo "<td>" . $viewrecord['booking_t1hols'] . " </td>";
      echo "<td>" . $viewrecord['booking_april'] . " </td>";
      echo "<td>" . $viewrecord['booking_may'] . " </td>";
      echo "<td>" . $viewrecord['booking_june'] . " </td>";
      echo "<td>" . $viewrecord['booking_t2hols'] . " </td>";
      echo "<td>" . $viewrecord['booking_july'] . " </td>";
      echo "<td>" . $viewrecord['booking_august'] . " </td>";
      echo "<td>" . $viewrecord['booking_september'] . " </td>";
      echo "<td>" . $viewrecord['booking_t3hols'] . " </td>";
      echo "<td>" . $viewrecord['booking_oct'] . " </td>";
      echo "<td>" . $viewrecord['booking_nov'] . " </td>";
      echo "<td>" . $viewrecord['booking_dec'] . " </td>";
      echo "<td>" . $viewrecord['booking_t4hols'] . " </td>";
      echo "<td>" . "<input type=submit class=form-control name=update value=update" . " </td>";
echo "</form>";
    }

    ?>

【问题讨论】:

  • 这是一个 SQL 问题,我建议添加 sql 标签并包含您尝试过的 select 语句。
  • 你能展示你的PHP代码吗?
  • @MosesSchwartz 我已将其添加到原作中
  • @Accountant 我不这么认为
  • @MosesSchwartz 好的,对不起,我误解了你的问题,我以为你正在努力编写 SQL 查询来选择你想要的数据。

标签: php mysql


【解决方案1】:

我不知道你是如何取回数据的,但这里有一些想法:

遍历所有结果,如果此日期,则获取月份编号,对于每个结果,在每个 tr 中添加一个 tr,为每个月份列添加 12 个 td,当循环中的 $i=== 到您的月份编号,添加到 $html

查看代码示例:

<table class="table table-bordered table-sm small">
  <thead class="thead-dark">
  <tr>
      <th>Jan</th>
      <th>Feb</th>
      <th>Mar</th>
      <th>Apr</th>
      <th>May</th>
      <th>Jun</th>
      <th>Jul</th>
      <th>Aug</th>
      <th>Sep</th>
      <th>Oct</th>
      <th>Nov</th>
      <th>Dec</th>
  </thead>
  </tr>
<?php 
$html = "";
require 'db.php';
$sqlview="SELECT * FROM booking";
$myData = mysqli_query($con, $sqlview);
//loop thru the dates (in your case the SQL results)
while($viewrecord = mysqli_fetch_array($myData)){
    //get the month number (Jan = 1, Feb = 2 etc.)
    $monthNumber = date_parse_from_format("y-m-d", $viewrecord['date'])['month'];
    //return A tr for each result
    $html .= '<tr>';
    //a td for each month
    for($i = 0; $i < 12; $i++){
        $html .= '<td>';
        //if column is current month, display date
        $html .= ($monthNumber === ($i+1)) ? $viewrecord['date'] : "";
        $html .= '</td>';
    }
    $html .= '<tr>';
}
echo $html;
?>
<table>

【讨论】:

【解决方案2】:

假设您正在使用 PDO 查询您的数据并且您已经选择了所有记录, 您需要做的就是使用 PHP 通常像下面的任何数组一样处理主要查询结果:

1- 您的查询:

$data = $pdo->query("SELECT * FROM TABLE")->fetchAll();

2- 创建月份数组(把你想要的所有月份):

$myMonth = array('January' => 'Jan','February' => 'Feb','March' => 'Mar');

3- 根据 $data 为每个月创建数组

$January = array_filter($data ,function($val){
    if (in_array($val["Jan"], $myMonth)) {
        return $val;
    }
});

我实际上并没有测试上面的代码,但重要的是我希望你明白了。

在每个月重复此操作后,您最终会得到 12 个单独的数组,其中包含每个数组所需的所有记录,然后您可以在表中将它们循环到每个月。

【讨论】:

  • 哇,就像我之前说的那样,我是初学者,学习速度很快。现在这有点过头了。 :-(但我会尝试
  • 太好了,作为初学者,您需要始终记住的主要事情是考虑您正在处理的对象的类型,一旦您定义了对象类型,您就可以开始搜索所有可用的方法来处理它然后找到你的解决方案。祝冰岛人好运
猜你喜欢
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 2021-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-03
  • 1970-01-01
相关资源
最近更新 更多