隐藏包含指定字符串的表行

Hide table row that contains specified string

本文关键字:字符串 包含指 隐藏      更新时间:2023-09-26

$row['attended']可以是'YES'或'NO'(字符串)。我想隐藏每一行有"出席"为"否"。在伪代码中:

if (<tr> contains 'NO') {
    hide that row (<-That's the tricky part to me)
    }

我的代码在服务器端,但如果有必要的话,我很乐意回显一些JavaScript。

<?php
include 'connect2.php';
date_default_timezone_set('Europe/Lisbon');
$today = date('Y-m-d');
$sql="Select * FROM detentions WHERE date = '$today'";
$result = mysqli_query($con,$sql);
echo "<center><table style='width:400px; border:3px solid #444'>
    <tr>
    <th style='background-color:#444'>First</th>
    <th style='background-color:#444'>Last</th>
    <th style='background-color:#444'>Attended</th>
    <th style='background-color:#444'></th>
    </tr>";
         while($row=mysqli_fetch_array($result)){
            echo "<tr>";
            echo "<td>" . $row['fname'] . "</td>";
            echo "<td>" . $row['lname'] . "</td>";
            echo "<td><center>" . $row['attended'] . "</center></td>";
            echo "<td><button type = 'button' class='unattended button-error pure-button button-xsmall' style='float:right; margin-left:0.2cm' name='" . $row['lname'] . "' id='" . $row['fname'] . "'><b>Unattended</b></button> <button type = 'button' class='editDet2 button-success pure-button button-xsmall' style='float:right' id=" . $row['det_id'] . "><b>Attended</b></button></td>";
            echo "</tr>";
  }
echo "</table>";    
mysqli_close($con);
?> 

另一个选择是在while语句中跳过$row['attended']="No"的记录…一个"if ($result.attended=='yes') {add…语句"will do…"虽然更有效和优雅的方法是在查询语句上添加子句,如:

Select * FROM detention WHERE date =' $today' and attended='yes'"

替换select语句

$sql="Select * FROM detentions WHERE date = '$today' AND attended = 'YES' OR attended = ''";

您可以在SELECT语句中添加WHERE子句,它指定出席字段应该等于no。这将首先防止不需要的行被返回。

修改SELECT语句为:SELECT * FROM detentions WHERE date = '$today' AND attended='yes'