php - 选择最后一行并填充整列 - PHP
问题描述
我的 PHP 代码正在选择最后一个日期并将其优先级设置为“低”,这是正确的 .. 但是,它也会用不正确的“低”填充所有行。其中,每一行应该代表他们自己的优先级值,如下所示:
但是,当我为相同的代码创建一个单独的文件时,它可以完美地工作:
这是它的工作代码: https ://pastebin.pl/view/740c956e
以下是不起作用的代码
<?php
$formdate = mysqli_query($dbconn, "SELECT formdate,form_id FROM `forms`" );
$currentDate = date('Y/m/d');
while ($row = $formdate->fetch_assoc()) {
$formd = $row['formdate'];
$days7= date('Y-m-d', strtotime($formd. ' + 7 days'));
$days14= date('Y-m-d', strtotime($formd. ' + 14 days'));
$newCurrentDate = strtotime($currentDate);
$newDays14 = strtotime($days14);
$newDays7 = strtotime($days7);
if (($newCurrentDate < $newDays14) and ($newCurrentDate < $newDays7 )) {
$priority = "Low";
}
elseif (($newCurrentDate > $newDays14) and ($newCurrentDate > $newDays7 )) {
$priority = "High";
}
elseif (($newCurrentDate > $newDays7) or ($newCurrentDate < $newDays14 )) {
$priority = "Normal";
}
}
function priority() {
$GLOBALS['priority'] = $priority;
}
?>
<table id="" class="table table-condensed table-striped">
<tr>
<th>Form Date</th>
<th>Form ID</th>
<th>Student ID</th>
<th>Form Type</th>
<th>Priority</th>
<th>Option</th>
<th></th>
</tr>
<?php
if($result){
while($rows = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$rows['formdate']."</td>";
echo "<td>".$rows['form_id']."</td>";
echo "<td>".$rows['student_id']."</td>";
echo "<td>".$rows['form_type']."</td>";
echo "<td>".$priority."</td>"; // This displays the priority value
echo "<td><a href=\"admin_form_view.php?form_id=$rows[form_id]\">View</a></td>";
echo "</tr>";
}
}?>
解决方案
formdate
您可以使用当前行的参数创建函数。然后检查时间间隔并返回优先级标签
<?php
function getPriority($formdate) {
$formd = $row['formdate'];
$days7= date('Y-m-d', strtotime($formd. ' + 7 days'));
$days14= date('Y-m-d', strtotime($formd. ' + 14 days'));
$newCurrentDate = strtotime($currentDate);
$newDays14 = strtotime($days14);
$newDays7 = strtotime($days7);
if (($newCurrentDate < $newDays14) and ($newCurrentDate < $newDays7 )) {
return "Low";
} elseif (($newCurrentDate > $newDays14) and ($newCurrentDate > $newDays7 )) {
return "High";
} elseif (($newCurrentDate > $newDays7) or ($newCurrentDate < $newDays14 )) {
return "Normal";
}
}?>
<?php
if($result){
while($rows = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$rows['formdate']."</td>";
echo "<td>".$rows['form_id']."</td>";
echo "<td>".$rows['student_id']."</td>";
echo "<td>".$rows['form_type']."</td>";
echo "<td>".getPriority($rows['formdate'])."</td>";
echo "<td><a href=\"admin_form_view.php?form_id=$rows[form_id]\">View</a></td>";
echo "</tr>";
}
}?>
推荐阅读
- css - 是否可以在反应中使用 css 自定义 FullCalendar?
- javascript - 将 Mousetrap Hotkey 绑定到某些 DOM 元素 - 仅在 div 处于活动状态时触发?
- flutter - 颤振错误:任务':app:compileDebugJavaWithJavac'的执行失败
- jquery - 点击后将 UL/LI 替换为 OPTION/SELECT
- javascript - 来自节点模块导入的意外令牌错误
- bluetooth-lowenergy - 如何开始使用 Nordic nRF52810 蓝牙 5.0 模块的初学者
- c# - 从服务器获取日期和时间
- azure - 创建 Azure SQL Server VM,包括备份
- jenkins - 由非管理员为 Jenkins 作业设置 cron 计划
- java - 默认导航抽屉活动的问题