简体   繁体   中英

PHP hiding div on empty result from database

I need to hide a full div if variable from database is empty.

I've looked at different similar cases but could not make any of them working. Perhaps someone could help me with it.

So I have my SELECT statement:

<?php
$user = 'myuser';
$pass = 'mypassword';
$db = new PDO( 'mysql:host=localhost;dbname=mydb', $user, $pass );
$sql = "
SELECT id,education_school,education_diploma, DATE_FORMAT(education_start, '%Y %M') as education_start, DATE_FORMAT(education_finish, '%Y %M') as education_finish, education_description
FROM candidates_education
WHERE login='[login]'
ORDER BY education_finish DESC
";
$query = $db->prepare( $sql );
$query->execute();
$results = $query->fetchAll( PDO::FETCH_ASSOC );
?>  

And then I have my html (part of it below):

<?php foreach ($results as $row) : ?>
<tr>
<td style="padding-left:20px;">
<div class="row invoice-cust-add">
<div class="col-xs-12" style="margin-top:-25px;"   <?php 
if(empty($education_description)||!isset($education_description)) echo 'style="display: none;"'; ?>   >
<p class="invoice-desc" style="line-height:23px; margin-bottom:-30px;"><span class="bold">Addtional description:</span><br/> <?php echo $row['education_description']; ?></p>
</div>                              
</div>  
</td>
</tr>
<?php endforeach; ?>

The problem I am having is that the div class="col-xs-12" should not be displayed if 'education_description' is empty. What am I doing here wrong?

Thank you in advance for any help

Change your HTML code like this:

<?php foreach ($results as $row) : 
        $isEmpty = empty($education_description); ?>
        <tr>
            <td style="padding-left:20px;">
                    <div class="row invoice-cust-add">
                    <div <?php if(!$isEmpty): ?> class="col-xs-12" <?php endif; ?> style="margin-top:-25px;"   <?php 
                        if($isEmpty || !isset($education_description)) echo 'style="display: none;"'; ?>   >
                        <p class="invoice-desc" style="line-height:23px; margin-bottom:-30px;"><span class="bold">Addtional description:</span><br/> <?php echo $row['education_description']; ?></p>
                    </div>                              
                </div>  
            </td>
        </tr>
<?php endforeach; ?>

Don't Panic's solution with

$row['education_description']

worked after I removed unnecessary part

style="margin-top:-25px;"

Thank you ALL for help! Your time is much much appreciated

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