簡體   English   中英

在儀表板中顯示數字,每年從 1 開始

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

我有一個表格,我希望在我的 .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表中的所有數據都不會從數據庫中刪除,狀態只會更改為刪除,僅此而已。

首先,如果你想讓你的記錄年復一年地排序,你還必須按date字段排序!

"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>

基於@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