[英]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.