简体   繁体   English

将来自数据库的图像显示为PHP中div元素的背景

[英]Display image from database as background of a div element in php

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! 当前,我正在学习PHP和数据库,我在下面编写了脚本,以将图像显示为div元素的背景,但输出实际上什么都没有! 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>

You are getting a little mixed up. 你有点困惑。 You already have PHP opening tags, you don't need them again. 您已经有了PHP的开始标记,您不再需要它们。 Just concatenate your variable: 只需连接变量:

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

Note: You also need to close your opening <div> tag. 注意:您还需要关闭打开的<div>标签。

There is a mistake in the line 生产线有误

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

You are already in php, so the opening tags are wrong there. 您已经在php中,因此此处的开始标记错误。

Try: 尝试:

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

Try this 尝试这个

 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>";
}

Replace this 取代这个

with this 有了这个

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

You actually did it right in the following echo commands. 您实际上在以下echo命令中正确地做到了。 To combine the value of your variable and a string you use the "." 要组合变量的值和字符串,请使用“。”。 that basically substitutes for "+" that is used in other languages for this purpose. 它基本上替代了为此目的在其他语言中使用的“ +”。 Opening a new php tag is therefore not needed here. 因此,这里不需要打开新的php标签。

The correct code is 正确的代码是

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

在php标记之外尝试此操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM