简体   繁体   English

在仪表板中显示数字,每年从 1 开始

[英]display number in dashboard to start from 1 every year

I have a table that I wish to display in my .php file, as it will display every number of each row but when it comes to a brand new year, it will changes to begin at 1 once again.我有一个表格,我希望在我的 .php 文件中显示,因为它会显示每行的每个数字,但是当谈到全新的一年时,它将再次从 1 开始。

.php file html code: .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>

What kind of javascript functions I must implement to make this work?我必须实现什么样的 javascript 函数才能使其工作?

NOTE: all the data inserted into the MySQL table will not be removed from the database, the status will only change to delete, that's all.注意:插入到MySQL表中的所有数据都不会从数据库中删除,状态只会更改为删除,仅此而已。

First of all, If you want your records to be sorted year after year, you also have to sort by the date field!首先,如果你想让你的记录年复一年地排序,你还必须按date字段排序!

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

And secondly you will have to remember your previous year in a loop, and compare it to the current.其次,您必须循环记住您的前一年,并将其与当前进行比较。 If it is different, then reset your ID to 1如果不同,则将您的 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>

Based on @Dmitriy Gritsenko's Help I've managed to did it, but the problem is that the other data from the columns of the table couldn't be display基于@Dmitriy Gritsenko 的帮助,我设法做到了,但问题是无法显示表列中的其他数据

<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

                include('include/config1.php');
                $query = "SELECT * from table ORDER BY sdate DESC, 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;                     
                }
                foreach ($data as $year => $oneYear) {
                    for ($i = count($oneYear); $i >= 1; $i--) {
            ?>
            <tr>
                <td><?= $year ?></td>
                <td><?= $i ?></td>
                <td><?php echo $row['Machine_No']; ?></td>
                <td><?php echo $row['projectName']; ?></td>
                <td><?php echo $row['pic']; ?></td>

                <td><?php 
                   $row['Purpose_of_Service'] =substr( $row['Purpose_of_Service'],0,50);
                    echo (str_replace('\r\n', "\r\n", $row['Purpose_of_Service'])); 
                    ?>
                </td>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM