字段为空时如何使用类隐藏DIV?

时间:2016-01-05 04:34:59

标签: javascript php jquery html css

我的页面上有一个PHP While 语句。我想隐藏DIV的名称"优惠券"对于每个具有空字段$rows['deal'];的记录。这是我的代码目前的样子。我怎样才能改变这一点。非常感谢!!!

<?php while ($rows = mysql_fetch_array($query)) { ?>        <!-- QUERY FOR PAGE 1 RECORDS -->
<div id="main">
<div id="client_name"><?php echo $rows['client_name']; ?></div>
<div id="phone"><?php echo $rows['phone']; ?></div>
<div id="client_square"></div>
<div id="client_rectangle"></div>
<img id="client_img" src="<?php echo $rows['client_img']; ?>">
<a id="edit" href="update_edit.php?id=<?php echo $rows['id']; ?>"><img id="edit_img" src="images/edit_record.png"></a>
<span class="coupon" id="cash_img"></span><span class="coupon" id="deal_text"><?php echo $rows['deal']; ?></span>
</div>
<?php } ?>  

7 个答案:

答案 0 :(得分:2)

你可以这样做:

<?php if($rows['deal']){ ?>
<span class="coupon" id="cash_img"></span><span class="coupon" id="deal_text"><?php echo $rows['deal']; ?></span>
<?php } ?>

完整代码

<?php while ($rows = mysql_fetch_array($query)) { ?>        <!-- QUERY FOR PAGE 1 RECORDS -->
<div id="main">
<div id="client_name"><?php echo $rows['client_name']; ?></div>
<div id="phone"><?php echo $rows['phone']; ?></div>
<div id="client_square"></div>
<div id="client_rectangle"></div>
<img id="client_img" src="<?php echo $rows['client_img']; ?>">
<a id="edit" href="update_edit.php?id=<?php echo $rows['id']; ?>"><img id="edit_img" src="images/edit_record.png"></a>
<?php if($rows['deal']){ ?>
<span class="coupon" id="cash_img"></span><span class="coupon" id="deal_text"><?php echo $rows['deal']; ?></span>
<?php } ?>
</div>
<?php } ?>

答案 1 :(得分:1)

尝试以下代码:

<?php while ($rows = mysql_fetch_array($query)) { ?>
    <!-- QUERY FOR PAGE 1 RECORDS -->
    <div id="main">
        <div id="client_name"><?php echo $rows['client_name']; ?></div>
        <div id="phone"><?php echo $rows['phone']; ?></div>
        <div id="client_square"></div>
        <div id="client_rectangle"></div>
        <img id="client_img" src="<?php echo $rows['client_img']; ?>">
        <a id="edit" href="update_edit.php?id=<?php echo $rows['id']; ?>"><img id="edit_img" src="images/edit_record.png"></a>
        <?php if($rows['deal']){ ?>
            <span class="coupon" id="cash_img"></span><span class="coupon" id="deal_text"><?php echo $rows['deal']; ?></span>
        <?php } ?>
    </div>
<?php } ?>

答案 2 :(得分:0)

您不需要javascript,添加以下行:

<?php echo ($rows['deal']==""?"style='display:none;'":"") ?>

完整代码:

<?php while ($rows = mysql_fetch_array($query)) { ?>        <!-- QUERY FOR PAGE 1 RECORDS -->
<div id="main">
<div id="client_name"><?php echo $rows['client_name']; ?></div>
<div id="phone"><?php echo $rows['phone']; ?></div>
<div id="client_square"></div>
<div id="client_rectangle"></div>
<img id="client_img" src="<?php echo $rows['client_img']; ?>">
<a id="edit" href="update_edit.php?id=<?php echo $rows['id']; ?>"><img id="edit_img" src="images/edit_record.png"></a>
<span class="coupon" <?php echo ($rows['deal']==""?"style='display:none;'":"") ?> id="cash_img"></span><span class="coupon" id="deal_text"><?php echo $rows['deal']; ?></span>
</div>
<?php } ?>  

答案 3 :(得分:0)

使用PHP

您可以只计算行是否有数据,以便回显它: 这是包含数据的行:

<?php
        $row = 'you can add get rows from database';
        if (count($row)) {
            ?>
            <div style="color: red">
                <?php echo $row;  // here you can add your row (foreach) ?>
            </div>
        <?php } ?>

这是没有数据的行

  <?php
    $row = '';
    if (count($row)) {
        ?>
        <div style="color: red">
            <?php echo $row;  // here you can add your row (foreach) ?>
        </div>
    <?php } ?>

答案 4 :(得分:0)

如果该字段为空,则其长度为0.因此,您可以使用该逻辑来隐藏“优惠券”类

    <?php while ($rows = mysql_fetch_array($query)) { ?>        <!-- QUERY FOR PAGE 1 RECORDS -->
    <div id="main">
    <div id="client_name"><?php echo $rows['client_name']; ?></div>
    <div id="phone"><?php echo $rows['phone']; ?></div>
    <div id="client_square"></div>
    <div id="client_rectangle"></div>
    <img id="client_img" src="<?php echo $rows['client_img']; ?>">
    <a id="edit" href="update_edit.php?id=<?php echo $rows['id']; ?>"><img id="edit_img" src="images/edit_record.png"></a>

<!-- My code -->
    <?php
    if(strlen($rows['deal'] > 0)
        {
    ?>
    <span class="coupon" id="cash_img"></span><span class="coupon" id="deal_text"><?php echo $rows['deal']; ?></span>
    <?php
        }
    ?>
<!-- End of my code --> 
    </div>
    <?php } ?>  

答案 5 :(得分:0)

我们可以使用这样的东西。

<?php if($fildvalue != ''){ ?>
<div><?php  echo $fildvalue; ?> </div>
<?php } ?>

所以div只有在值为

时才会被创建

答案 6 :(得分:0)

检查数据行并放入要隐藏的所有HTML标记,以防它是空的或未设置。

<?php if(isset($rows['deal']) && !empty($rows['deal'])){ ?>
    <span class="coupon" id="deal_text"><?php echo $rows['deal']; ?></span>
<?php } ?>

在PHP中,empty()函数能够检查 NULL值空字符串作为回报。