dqf60304 2019-06-20 10:29
浏览 55
已采纳

来自sql表的图像不遵循css

For the images for my website I take them from my sql table. The majority of the images are working and style how they are supposed to. However, the image for my header background is not and I cannot figure out why since the other images work fine.

This is the code for the header background which shows how I take the image from my sql table:

<header class="background -display-container -grayscale-min" id="home">
        <?php
        try {
        $stmt = $db->query('SELECT image_title, image FROM images WHERE id= 1 ');
        while($row = $stmt->fetch()){
        echo "<img src='admin/uploads/".$row['image']."'";
        }
    }catch(PDOException $e) {
                echo $e->getMessage();
            }
?> 

  <div class="logan-display-left slogan" style="padding:48px">
    <span class="-jumbo -hide-small">Baking for you since '62</span><br>
    <p><a href="#about" class="-button -white -padding-large -large -margin-top -opacity -hover-opacity-off">Learn more</a></p>
  </div> 
</header>

This is the css it is supposed to follow:

/* Full height image header */
.background {
  background-position: center;
  background-size: cover;
  min-height: 100%;
}
  • 写回答

1条回答 默认 最新

  • dongling5411 2019-06-20 10:34
    关注

    I think this is how it should be:

    <header class="background -display-container -grayscale-min" id="home" style="background-image: url(
            <?php
            try {
            $stmt = $db->query('SELECT image_title, image FROM images WHERE id= 1 ');
            while($row = $stmt->fetch()){
            echo "admin/uploads/".$row['image'];
            }
        }catch(PDOException $e) {
                    echo $e->getMessage();
                }
    ?>">
    

    So, instead of producing <img> tag, background-image CSS attribute is set. Then your background class does the rest.

    Update. Well, while we're at it, here is a bit better version of the code with image URL fetching functionality extracted into reusable function:

    <?php
    function imageUrl($id) use ($db)
    {
        try {
            $stmt = $db->query('SELECT image FROM images WHERE id=  ' . (int)$id);
            $row = $stmt->fetch();
            return "admin/uploads/".$row['image'];
        } catch(PDOException $e) {
            // This way you at least will see error in your server logs, and probably in browser console:
            return $e->getMessage();
        }
    }
    ?>
    <header class="background -display-container -grayscale-min" id="home" style="background-image: url(<?= imageUrl(1) ?>;">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题