我有这个基本的搜索系统,在“卡片”中显示文章 here 有些文章包含图片,有些则没有。
我只想显示“图片框”div
当里面有图片时,instead of having a blank white space when there's no picture.
如何只删除里面没有图像的“图片框”div?
PHP:
$sql = "SELECT * FROM article ORDER BY a_id DESC LIMIT 3";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if ($queryResults > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<a href='article.php?title=".$row['a_title']."&date=".$row['a_date']."'>
<div class='article-wrapper'>
<div class='picture-box'>
<img src='".$row['picture']."' class='picture'>
</div>
<p class='text'>".$row['text']."</p>
</div>
</a>";
}
}
CSS:
.picture {
position:absolute;
left: -100%;
right: -100%;
top: -100%;
bottom: -100%;
margin: auto;
max-width: 100%;
height: auto;
}
.picture-box {
width: 560px;
height: 300px;
overflow: hidden;
position: relative;
}
我试过这个:
PHP:
<img src='".$row['picture']."' class='picture' onerror="imgError(this)">
j查询:
function imgError(img) {
img.error="";
$(img).parent().remove();
}
-但它会从所有文章而不是特定文章中删除“图片框”div。
最佳答案
为什么还要先创建 div 并依赖 jQuery 来清理困惑?只需在最初创建页面时向您的 PHP 添加一个 if 语句,如果没有值(value)则不要添加图片。
echo "<a href='article.php?title=".$row['a_title']." date=".$row['a_date']."'>
<div class='article-wrapper'>";
if(trim($row['picture']) != ''){
echo "<div class='picture-box'>
<img src='".$row['picture']."' class='picture'>
</div>";
}
echo "<p class='text'>".$row['text']."</p>
</div>
</a>";
关于php - 删除具有特定条件的行内的div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48835213/