zhak
zhak

Reputation: 21

Adding PHP to background-image url

This is what I have so far but it's not working. This is my 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.

clicke to view output screenshot ,the blank space is the error ,others are url images

Upvotes: 1

Views: 5891

Answers (4)

zhak
zhak

Reputation: 21

thank y'all for your contributions

i got this myself

i just added my website address to it @https://mywebsite.com/upload/blog/...

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

Upvotes: 0

Abdul Hannan
Abdul Hannan

Reputation: 1

Use double quote here...

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

enter image description here

Upvotes: 0

Pinke Helga
Pinke Helga

Reputation: 6682

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.

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

    </article>
<?php
  }

Upvotes: 3

Johannes
Johannes

Reputation: 67748

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:

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

Upvotes: 1

Related Questions