Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择最后一行并填充整个列-PHP_Php - Fatal编程技术网

选择最后一行并填充整个列-PHP

选择最后一行并填充整个列-PHP,php,Php,当前结果: 我的PHP代码正在选择最后一个日期,并将其优先级设置为“低”,这是正确的。。但是,它也会用不正确的“Low”填充所有行。其中,每行应表示其自身的优先级值,如下所示: 但是,当我为同一代码创建一个单独的文件时,它可以完美地工作: 这是它的工作代码: 下面是不起作用的代码 <?php $formdate = mysqli_query($dbconn, "SELECT formdate,form_id FROM `forms`" ); $curre

当前结果:

我的PHP代码正在选择最后一个日期,并将其优先级设置为“低”,这是正确的。。但是,它也会用不正确的“Low”填充所有行。其中,每行应表示其自身的优先级值,如下所示:

但是,当我为同一代码创建一个单独的文件时,它可以完美地工作:

这是它的工作代码:

下面是不起作用的代码

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


表格日期
表格ID
学生证
表格类型
优先
选项

您可以使用当前行的参数
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>"; 
                                                        }
                                                      }?>


您需要为显示的每一行计算出
$priority
,此时只需计算一次并始终显示该值。所以,在
echo“$priority”行之前计算出它使用
$rows
中的值。步骤1:格式化代码,以便您(和我们)可以清楚地看到循环内部和外部的内容。第2步:硬编码输入,这样您就可以创建一个。第3步:删除除优先级之外的所有逻辑和输出,这样您就可以专注于该部分了。