Javascript 在仪表板中显示数字,以每年从1开始
我希望在我的.php文件中显示一个表,因为它将显示每行的每一个数字,但当涉及到全新的一年时,它将再次更改为从1开始 .php文件html代码:Javascript 在仪表板中显示数字,以每年从1开始,javascript,php,html,mysql,Javascript,Php,Html,Mysql,我希望在我的.php文件中显示一个表,因为它将显示每行的每一个数字,但当涉及到全新的一年时,它将再次更改为从1开始 .php文件html代码: <table id="example"> <thead> <tr> <th style="width: 40px">Year</th> <th style="width: 40px">Id<
<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表中插入的所有数据不会从数据库中删除,状态只会更改为删除,仅此而已。首先,如果您希望记录年复一年地排序,您还必须按日期字段排序 第二,你必须在一个循环中记住你的上一年,并将其与当前进行比较。如果不同,则将ID重置为1
基于@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>
JavaScript?这不是一个简单的循环吗?循环?从mysql中取出后如何循环?好吧,就像php中的其他循环一样,对吗?就像有增量一样,但当它进入全新的一年时,它将再次从1变为1,可以吗?嗨!它工作了,但我希望它以desc顺序显示id增量,而不是从最新的to行开始1@Lim我已经更新了答案,以降序显示记录。如果确实对你有帮助,请考虑赞成投票。当然!id列中显示的增量id仍然从1开始,因为您所更改的只是表中id在desc中显示的顺序,这是正确的,但最早的数据显示是当年行的最大计数,最新的显示是1,这就是问题所在,我希望一年中最新的1d是一年中的最大增量,最早的id从1开始,从表顺序中选择*由sdate DESC显示最新的一年即可out@Lim在本例中,我建议将结果存储在temporarry数组$data中,并通过it元素向后循环。请查看我的更新答案。我已根据您的其他信息更新了我的答案
<?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>
<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>