javascript - 每年在仪表板中显示的数字从 1 开始

标签 javascript php html mysql

我希望在 .php 文件中显示一个表格,因为它将显示每行的每个数字,但当进入新的一年时,它会再次从 1 开始。

.php文件html代码:

<table id="example">
      <thead>
             <tr>
             <th style="width: 40px">Year</th>
             <th style="width: 40px">Id</th>
             </tr>
     </thead>
<tbody>
<?php
include('include/config1.php');
$query = "SELECT * from table ORDER BY Id DESC";
$result = mysqli_query($db, $query);
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php
     if($row['status']!=3){ //display the year from the date according to status
       echo date("Y",strtotime($row['sdate'])); }
     else{
       echo date("Y",strtotime($row['ssdate']));
     } ?>
     //the years are the same for both columns in each row
</td>

<td>//The ID that auto starts from 1 every year with an increment of 1 to the newest (not the same as the unique id from the table </td>

<?php}?>
</tbody>
</table>

我必须实现什么样的 JavaScript 函数才能完成这项工作?

注意:所有插入MySQL表的数据都不会从数据库中删除,状态只会改变为删除,仅此而已。

最佳答案

首先,如果您希望记录年复一年地排序,您还必须按日期字段排序!

"SELECT * from table ORDER BY sdate,Id DESC";

其次,你必须循环记住前一年的情况,并将其与当前的情况进行比较。如果不同,请将您的 ID 重置为 1

<?php
    include('include/config1.php');
    $query = "SELECT * from table ORDER BY sdate, Id DESC";
    $result = mysqli_query($db, $query);

    $currentYear = null;
    $data = [];
    while ($row = mysqli_fetch_assoc($result)) {
        $year = ($row['status'] == 3) ? date("Y", strtotime($row['ssdate'])) : date("Y", strtotime($row['sdate']));

        $data[$year][] = $row;                     
    }
?>

<table id="example">
<thead>
    <tr>
        <th style="width: 40px">Year</th>
        <th style="width: 40px">Id</th>
        <th style="width: 80px">Machine No</th>
        <th style="width: 80px">Project Name</th>
        <th style="width: 80px">PIC</th>
        <th style="width: 80px">Purpose of Service</th>
    </tr>
</thead>
<tbody>
<?php 
    foreach ($data as $year => $oneYear) {
        for ($i = count($oneYear); $i >= 1; $i--) {
?>
    <tr>
        <td><?= $year ?></td>
        <td><?= $i ?></td>
        <td><?= $oneYear[$i]['Machine_No']; ?></td>
        <td><?= $oneYear[$i]['projectName']; ?></td>
        <td><?= $oneYear[$i]['pic']; ?></td>
        <td><?= substr(str_replace('\r\n', "\r\n", $row['Purpose_of_Service']), 0, 50); ?></td>
    </tr>
<?php
        }
    }
?>
</tbody>
</table>

关于javascript - 每年在仪表板中显示的数字从 1 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60716125/

相关文章:

javascript - 引用外部类中的 div

javascript - AngularJS SPA 中 Google Chrome 控制台中的奇怪请求失败

javascript - 使用 AJAX 检查用户名可用性

javascript - 显示 map ,同时通过 ajax/php 从数据库检索坐标

javascript - HTML 元素的本地 id-s

javascript - 在传递参数时在 SSRS 中的新窗口中打开 URL

javascript - 检测请求是否是 jQuery 中的帖子

PHP 覆盖文件中的特定位置

html - 使用绝对定位的子div时如何扩展绝对定位的包装div?

html - 如何用背景色填充整个div