dou44364983 2014-07-17 09:08
浏览 50

从数据库显示图像作为php中div元素的背景

Currently I'm learning about PHP and database, I wrote the script below to display the image as background for my div element, but the output is actually nothing! the error is from the line:

echo "<div class=\"post\" style='background-image: url('\"<?php echo $data[Image];\"?>')'";?>

The quotes mess up! Can someone tell me how to correct this? I tried to change double quotes to single quotes, but still doesn't work at all.

This is my full script:

<div class="dashboardA">
                <?php
                    $con = mysqli_connect("localhost", "Dave", "password");
                    if (!$con){
                        die ("Could not connect to database: " . mysqli_connect_error());
                    }
                    mysqli_select_db($con, "my_blog");

                    $sql = mysqli_query($con, "select * from article");

                    while ($data=mysqli_fetch_array($sql)){
                        echo "<div class=\"post\" style='background-image: url('\"<?php echo $data[Image];\"?>')'";?>
                        <?php echo "<p>" . $data["Title"] . "</p>";
                        echo "<p>" . $data["Category"] . "</p>";
                        echo "<p>" . $data["Published"] . "</p>";
                        echo "</div>";
                    }
                ?>
            </div>
  • 写回答

5条回答 默认 最新

  • dongteng0748 2014-07-17 09:11
    关注

    You are getting a little mixed up. You already have PHP opening tags, you don't need them again. Just concatenate your variable:

    echo "<div class='post' style='background-image: url(\"$data[Image]\")'>";
    

    Note: You also need to close your opening <div> tag.

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真