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 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥15 Mabatis查询数据