PHP MYSQL 用foreach循环显示sql结果

问题描述 投票:0回答:1

我有一份已提交的车辆检查清单。检查按日期进行组织和分组。该表显示了日期以及提交支票的人。当我单击“查看”时,它应该仅显示该日期的所有检查、检查状态以及已提交检查的评论。

我将表格设置为表单,当单击“视图”时,它充当提交按钮以 POST 到另一个 php 文件以显示日期检查。

This is the first table 这显示了按日期列出提交的支票的表格。

$checkssql = " SELECT * FROM $appname2 GROUP BY date ORDER BY date DESC";
$checksqry = mysqli_query($conn, $checkssql);

<form name="submittedchecks" id="submittedchecks" method="POST" action="view-submitted-check.php">
   <div class="table-responsive text-nowrap">   
      <table class="table" >
         <thead>
            <tr>
            <th>Date</th>
            <th>Submitted By</th>
            </tr>
         </thead>
         <tbody class="table-border-bottom-0">
            <?php while ($checks = mysqli_fetch_array($checksqry)) {{
            echo '<input type=hidden name="checkdate[]" value='. $checks['date']. '>';
            echo '<input type="text" name="appname" id="appname" readonly value="'.$appname2.'" hidden="hidden">';
            echo '<tr>';
            echo '<td> <input type="text" value="'.$checks['date'].'" name="date[]" id="date" style="border: none" readonly></td>';
            echo '<td>'.$checks['submitted_by'].'</td>';
            echo "<td> <button class='btn btn-info' type='submit' name='submitview' id='submitview'>View</button> </td> " ;}
            if($row['user_type']=='admin'){  echo "<td> <a href=delete-truck-check-script.php?id=".$checks['id']."><button class='btn btn-danger' type='button' >Delete Check</button></a> </td> " ;} 
echo'</tr>'; }?>
      </table>
   </div>
</form>

这是获取发布数据并将其显示在所选支票日期的表格中的代码。

foreach ($_POST['checkdate'] as $index => $id){ 
    
$appname = $_POST['appname'];
$date = $_POST['date'][$index];
    
$viewlog = "SELECT * FROM $appname WHERE date = '$date' ORDER BY id";
$viewresult = mysqli_query($conn, $viewlog);
     
 }

<table class="table" >
   <thead>
      <tr>
      <th>Check Name</th>
      <th>Status</th>
      <th>Comment</th>
      </tr>
   </thead>
   <tbody class="table-border-bottom-0">
      <?php while ($checkview = mysqli_fetch_array($viewresult)) {{
      echo '<tr>';
      echo '<td>' .$checkview['check_name']. '</td>';
      if($checkview['status']=='Pass'){  echo "<td> <label class='btn rounded-pill btn-outline-success'>Pass</label> </td> " ;} 
      if($checkview['status']=='Warning'){  echo "<td> <label class='btn rounded-pill btn-outline-warning'>Warning</label> </td> " ;}
      if($checkview['status']=='Fail'){  echo "<td> <label class='btn rounded-pill btn-outline-danger'>Fail</label> </td> " ;}
      echo '<td>' .$checkview['comment']. '</td>';}
      echo'</tr>'; }?>
</table>

我尝试了其他帖子的多个答案,但没有任何效果。我需要另一双眼睛来发现我的错误。

我将 $id 切换为 $index,但它只获得 1 个日期。 我点击了顶部的支票“03-21-2024”,但它显示了“03-20-2024”的支票 As you can see here

php mysql mysqli web-applications
1个回答
0
投票

您需要在

foreach
循环内移动表格。

我还展示了如何使用准备好的语句,而不是将

$date
替换到 SQL 中。

无需循环

$_POST['checkdate']
,因为您从未使用过它的值。只需循环
$_POST['date']
并使用这些输入即可。

$stmt = mysqli_prepare($conn, "SELECT * FROM $appname WHERE date = ? ORDER BY id");
mysql_stmt_bind_param($stmt, "s", $date);
    
$appname = $_POST['appname'];

foreach ($_POST['date'] as $date){ 
    mysqli_stmt_execute($stmt);
    $viewresult = mysqli_stmt_get_result($stmt);

    ?>
    <table class="table" >
       <thead>
          <tr>
          <th>Check Name</th>
          <th>Status</th>
          <th>Comment</th>
          </tr>
       </thead>
       <tbody class="table-border-bottom-0">
          <?php while ($checkview = mysqli_fetch_array($viewresult)) {
              echo '<tr>';
              echo '<td>' .$checkview['check_name']. '</td>';
              if($checkview['status']=='Pass'){  echo "<td> <label class='btn rounded-pill btn-outline-success'>Pass</label> </td> " ;} 
              elseif($checkview['status']=='Warning'){  echo "<td> <label class='btn rounded-pill btn-outline-warning'>Warning</label> </td> " ;}
              elseif($checkview['status']=='Fail'){  echo "<td> <label class='btn rounded-pill btn-outline-danger'>Fail</label> </td> " ;}
              echo '<td>' .$checkview['comment']. '</td>';
              echo'</tr>';
           }
          ?>
       </tbody>
    </table>
    <?php
 }
© www.soinside.com 2019 - 2024. All rights reserved.