php - 删除具有特定条件的行内的div

标签 php jquery css mysqli

我有这个基本的搜索系统,在“卡片”中显示文章 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/

相关文章:

jquery - MSIE : jQuery's append()/html() not working, 求助于 getElementById().innerHTML

javascript - div 的左侧元素为空字符串

php - 获取两个不同表mysql的结果

php - 何时使用 index.php 而不是 index.html

php - 如何在方法之间共享行为

javascript - 根据 data-* 属性过滤选择选项

javascript - 删除后减少 JSON 数组中的 ID

html - 两个网站上的相同字体系列以不同方式显示文本

html - 如何在css中创建两列

php - 停止远程服务器 PHP 上的无限循环