[英]PHP hiding div on empty result from database
如果数据库中的变量为空,则需要隐藏完整的div。
我看过不同的类似案例,但无法使它们中的任何一个起作用。 也许有人可以帮助我。
所以我有我的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 );
?>
然后我有我的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; ?>
我遇到的问题是,如果'education_description'为空,则不应显示div class =“ col-xs-12”。 我在这里做什么错了?
预先感谢您的任何帮助
像这样更改您的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; ?>
不要惊慌的解决方案
$row['education_description']
我删除了不必要的部分后工作
style="margin-top:-25px;"
谢谢大家的帮助! 非常感谢您的时间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.