首页 > 解决方案 > 选择最后一行并填充整列 - 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>"; 
                                            }
                                          }?>

标签: php

解决方案


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>"; 
                                                        }
                                                      }?>

推荐阅读