简体   繁体   English

PHP在数据库的空结果上隐藏div

[英]PHP hiding div on empty result from database

I need to hide a full div if variable from database is empty. 如果数据库中的变量为空,则需要隐藏完整的div。

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: 所以我有我的SELECT语句:

<?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): 然后我有我的html(下面的一部分):

<?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. 我遇到的问题是,如果'education_description'为空,则不应显示div class =“ col-xs-12”。 What am I doing here wrong? 我在这里做什么错了?

Thank you in advance for any help 预先感谢您的任何帮助

Change your HTML code like this: 像这样更改您的HTML代码:

<?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 非常感谢您的时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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