简体   繁体   中英

How to hide DIV using class when field is Empty?

I have a PHP While statement on my page. I would like to hide DIV with the class name "coupon" for each record that has empty field $rows['deal']; . This is what my code currently looks like. How can I alter to accomplish this. Thank You much!!!

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

You can do this on this way:

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

Full code

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

Try below code:

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

You do not need javascript for this, add this line:

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

Complete code:

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

with PHP:

You can just count if row has a data so echo it: this is row with data:

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

this is row with out data

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

If the field is empty, it will have a length of 0. So you can use that logic in order to hide the 'coupon' class

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

We can use something like this .

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

so div will be created only when their is an value

Check the data row and put all the HTML tags which you want to hide in case if it is empty or not set.

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

In PHP, empty() function is able to check NULL value , empty string and zero in return.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM