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

时间:2015-08-31 01:16:05

标签: javascript php

$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);
?> 

3 个答案:

答案 0 :(得分:2)

另一种选择是在你的while语句中跳过带有$ row ['attend'] =“No”的记录...“if($ result.attended =='yes'){add ...} stamement “会做...虽然更有效和优雅的方法是在查询语句中添加一个子句,如:

选择* FROM detentions WHERE date ='$ today'and attend ='yes'“

正如之前的回答所建议的那样......

答案 1 :(得分:1)

替换你的select语句

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

并删除你的if条件。

答案 2 :(得分:1)

您可以在 SELECT语句中添加 WHERE子句,指定参加者字段应等于否< / strong>即可。这样可以防止首先返回不需要的行。

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