简体   繁体   English

将 PHP 添加到背景图像 url

[英]Adding PHP to background-image url

This is what I have so far but it's not working.这是我到目前为止所拥有的,但它不起作用。 This is my css:这是我的CSS:

background-image: url("upload/blog/'.$row["urlImg"].'");

Here is the full code:这是完整的代码:

<?php 
  $stmt = $db->query
  ( 'SELECT blog.postID, blog.postTitle, blog.postSlug, blog.postDesc, blog.urlImg, blog.postTags, blog.postDate
     FROM blog, blog_post_cats
     WHERE blog.postID = blog_post_cats.postID AND blog_post_cats.catID = 4 ORDER BY postID DESC LIMIT 1'
  ); 
  while($row = $stmt->fetch())
  {
    echo '<article style="background-image: url(upload/blog/'.$row['urlImg'].'" );"> '; 
  }
?>

This PHP echo does not work.这个 PHP echo不起作用。

clicke to view output screenshot ,the blank space is the error ,others are url images点击查看输出截图,空格为错误,其他为url图片

Since you try to use double quotes in inline CSS, you actually are closing the HTML attribute style="background-image: url(" . Use single quotes in this case of nested quotes. Rather than escaping single quotes in single-qouted echo strings, just close <?php ?> tags and write plain HTML. 由于您尝试在内联CSS中使用双引号,因此实际上您正在关闭HTML属性style="background-image: url(" 。在嵌套引号的情况下,请使用单引号。而不是在单引出的echo字符串中转义单引号。 ,只需关闭<?php ?>标签并编写纯HTML。

<?php
  while($row = $stmt->fetch())
  {
?>
    <article class="latestPost excerpt big-1" style="background-image: url('upload/blog/<?php echo $row["urlImg"];?>');">

    </article>
<?php
  }

The problem are the double-quotes around the URL of the background image. 问题是背景图片的网址周围有双引号。 The double quote after url( closes the style atrribute it's part of. So you should use escaped quotes inside the url(...) parenthesis: url(后的双引号会关闭style的一部分。因此,您应该在url(...)括号内使用转义的引号:

echo '<article class="latestPost excerpt big-1" style="background-image: url(\"upload/blog/'.$row["urlImg"].'\");"> ';

Use double quote here... 在这里使用双引号...

     echo '<article style="background-image: url(upload/blog/'.$row["urlImg"].'");"> ';

enter image description here 在此处输入图片说明

thank y'all for your contributions 谢谢大家的贡献

i got this myself 我自己得到这个

i just added my website address to it @ https://mywebsite.com/upload/blog/... 我刚刚将我的网站地址添加到它@ https://mywebsite.com/upload/blog/...

 echo '<article style="background-image: url(https://mywebsite.com/upload/blog/'.$row["urlImg"].'");"> ';

为什么不为我工作这个代码

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

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