Search code examples
phpsqlinline-styles

Inline style in index.php does not display background image pulled from database


I have an index.php which I have successfully filled with content from the relevant tables in a mysql database. However there is one section (events) that has a background image that is defined in the css. However after changing the css to a php and making sure to give mention <?php header("Content-type: text/css; charset: UTF-8"); ?>, background-image:url:"<?php echo $eventsrow[0]['filepath'];?>" was throwing the style of the entire document out of whack apart from not showing the image.

So i decided to put the image call in the index.php instead, as I had done for the other sections, by using inline style: <div class="monthevent" style="background-image:url("<?php echo $eventsrow[0]['filepath'];?>");">

However the image does not appear at all. Is there something wrong with this? Would appreciate any light. A var_dump on the above variable, gives the correct value: ie. the filepath, but the image is not displayed; also I tried adjusting the path relative to the css (just in case), but no image turned up. Would appreciate any thoughts on what the issue might be?


Solution

  • You have to use like this:

    <div class="monthevent" style="background-image:url('<?php echo $eventsrow[0]['filepath'];?>');">
    

    Not the double quotes inside the url, use single quotes to differentiate the url!

    If you use double quotes:

    <div class="monthevent" style="background-image:url("your_url.com");">
    </div>
    

    If you use single quotes:

    <div class="monthevent" style="background-image:url('your_url.com');">
    </div>