1

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

9
  • 2
    Did you forget to add your code? 😅 Please edit your question. Commented Feb 1, 2019 at 23:58
  • 1
    I think the problem is improperly escaped quotes. Commented Feb 2, 2019 at 0:01
  • You need to edit your question to also show how you get $row["urlImg"] If its returning blank then the query (assuming since you are using $row) maybe be returning blank or you might be fetching it wrong Commented Feb 2, 2019 at 0:03
  • 1
    The first code snippet and the "full" code differ from each other. Unclear what is what. Commented Feb 2, 2019 at 0:12
  • 1
    Can you show us the html displayed on output when it "doesn't work" Commented Feb 2, 2019 at 0:45

4 Answers 4

3

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
  }
Sign up to request clarification or add additional context in comments.

2 Comments

i cannot us that simply because the code is run inside a query <?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'].'");"> '; ?>
What is the problem? You can close and reopen PHP tags almost everywhere.
1

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"].'\");"> ';

Comments

0

Use double quote here...

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

enter image description here

Comments

0

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"].'");"> ';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.