[英]Don't display the specific empty column of row from database
I have a working code that can insert and update information from the database and echoing it to page. 我有一个工作代码,可以插入和更新数据库中的信息并将其回显到页面。 but I like to hide the specific empty column while displaying all the information from the row.
但我想在显示行中的所有信息时隐藏特定的空列。 check this screenshot my goal is to hide the "image" column when its null/empty;
检查此屏幕截图,我的目标是在“图片”列为空/空时隐藏它; so the crock image wont display.
因此缸图像不会显示。 here is my code below:
这是我的代码如下:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM blogs ORDER BY id DESC");
while ($row = mysqli_fetch_array($result))
if (!empty($row['image'] != ""))
{
echo "<div class='row'>
<div class='col-lg-12 box'>
<div class='content-heading'>
<p>
<text>".$row['title']."</text>
</p>
</div>
<p>
<text1 class='pull-right' >".$row['image_text']."</text1><br/>
<img class='img-size' id='hp'src='admin/upload_images/".$row['image']."'/>
<text>".$row['definition']."</text>
</p>
</div>
</div>";
}
?>
but this code if (!empty($row['image'] != ""))
is hiding the entire row from my database. 但是
if (!empty($row['image'] != ""))
正在从我的数据库隐藏整个行, if (!empty($row['image'] != ""))
此代码。 Can anyone have a right solution to my problem? 谁能对我的问题找到正确的解决方案?
You could try using the style
attribute of the <img>
tag to selectively show or hide the image tag: 您可以尝试使用
<img>
标记的style
属性来选择性地显示或隐藏image标记:
<img class='img-size'
id='hp'
style='display: '. ($row["image"] != "" ? "block" : "none") . ';'
src='admin/upload_images/".$row['image']."'/>
Instead of hiding entire row, you could just hide the image tag, like this: 除了隐藏整个行,您还可以隐藏image标签,如下所示:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM blogs ORDER BY id DESC");
while ($row = mysqli_fetch_array($result))
{
echo "
<div class='row'>
<div class='col-lg-12 box'>
<div class='content-heading'>
<p>
<text>".$row['title']."</text>
</p>
</div>
<p>
<text1 class='pull-right' >".$row['image_text']."</text1><br/>
";
if (!empty($row['image'])) {
echo "<img class='img-size' id='hp'src='admin/upload_images/".$row['image']."'/>";
}
echo "
<text>".$row['definition']."</text>
</p>
</div>
</div>
";
}
?>
Your if condition will hide all other html elements as well instead you should hide only img
tag when no value found for image
. 您的if条件也会隐藏所有其他html元素,而当没有找到
image
值时,您应该只隐藏img
标签。
Update your code as below : 更新代码如下:
while ($row = mysqli_fetch_array($result)) {
$image = (!empty($row['image'])) ? "<img class='img-size' id='hp'src='admin/upload_images/" . $row['image'] . "'/>" : "";
echo "<div class='row'>
<div class='col-lg-12 box'>
<div class='content-heading'>
<p>
<text>" . $row['title'] . "</text>
</p>
</div>
<p>
<text1 class='pull-right' >" . $row['image_text'] . "</text1><br/>
" . $image . "
<text>" . $row['definition'] . "</text>
</p>
</div>
</div>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.