/    Sign up×
Community /Pin to ProfileBookmark

Echoing background-image inline style not working

why is background-mage not working inline style?

[code=php]<?php

echo “<div class=’paket_title’ style=’background-image: url(‘img1.jpg’);’>
$r[head]
</div>”;

?> [/code]

it works if the code is written inside a separate CSS file, but i need to write inline because I’m using this with Switch/case statement and each case will have different image
example :

[code=php]
<?php

$bg = $r[bgcolor];

switch ($bg) {
case “red” :
echo “<div class=’paket_title’ style=’background-image :url(‘img1.jpg’);’>$r[head]</div>”;
break;

case “blue” :
echo “<div class=’paket_title’ style=’background-image :url(‘img2.jpg’);’>$r[head]</div>”;
break;

case “yellow” :
echo “<div class=’paket_title’ style=’background-image :url(‘img3.jpg’);’>$r[head]</div>”;
break;
}

?>
[/code]

I tried rewriting the code, here is the list of my tries :

[code=php]
1.

echo “<div class=’paket_title’ style=’background: url(img1.jpg);’>
$r[head]
</div>”;

2.

echo “<div class=’paket_title’ style=’background-image: url(“img1.jpg”);’>
$r[head]
</div>”;

3.

echo ‘<div class=”paket_title” style=”background-image: url(“img1.jpg”);”>
$r[head]
</div>’;

4.

echo “<div class=’paket_title’ style=’background: url(/’img1.jpg/’);’>
$r[head]
</div>”;

5.

echo ‘<div class=”paket_title” style=”background: url(‘img1.jpg’);”>
$r[head]
</div>’;

[/code]

echoing both background and background-image does not work, but background-color works
anyone has solution for this?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJun 29.2016 — try alternating your quotes. One can't use single quotes inside single quotes!
Copy linkTweet thisAlerts:
@NogDogJun 29.2016 — Might be simpler to not be in PHP mode until you absolutely need to be:
[code=php]
<?php
// assorted PHP code, then...
?>
<div class="paket_title" style="background-image: url('img1.jpg');">
<?php echo $r['head']; ?>
</div>
<?php
// more PHP code, perhaps...
[/code]


PS: The above would be assuming that img1.jpg is in the same directory as this file, in case that's the issue and you need to use a relative or absolute path to it, instead?
Copy linkTweet thisAlerts:
@redflyshauthorJun 30.2016 — try alternating your quotes. One can't use single quotes inside single quotes![/QUOTE]

thank you for your suggestion. this was the code I've written before trying to alterante quotes, but even after I alternate quotes, it still now showing

Might be simpler to not be in PHP mode until you absolutely need to be:
[code=php]
<?php
// assorted PHP code, then...
?>
<div class="paket_title" style="background-image: url('img1.jpg');">
<?php echo $r['head']; ?>
</div>
<?php
// more PHP code, perhaps...
[/code]


PS: The above would be assuming that img1.jpg is in the same directory as this file, in case that's the issue and you need to use a relative or absolute path to it, instead?[/QUOTE]



thank you for taking time answering this. first of all this php code is for generating the content of an entire html page so I really need to put everything inside php

or is it really possible to do what you suggest?

oh and the image is in the same directory with php file, I even copy pasted the image to all directories of my website folder.

please take a look at my full code here :

[code=php]
<?php

// PAKET USAHA
if ($_GET['module']=='paketusaha'){
$paket = mysql_query("SELECT * FROM paket_usaha ORDER BY id_paket DESC LIMIT 5");
while ($r=mysql_fetch_array($paket)){
$id_panel=1;
$bg=$r['bgcolor'];
switch ($bg) {
case "red" :
echo "<div class='paket_title' style='background-image :url("img1.jpg"); '>" . $r['head'] . "</div>";
break;

case "blue" :
echo "<div class='paket_title' style='background-image :url("img1.jpg"); '>" . $r['head'] . "</div>";
break;

case "yellow" :
echo "<div class='paket_title' style='background-image :url("img1.jpg"); '>" . $r['head'] . "</div>";
break;


}


echo "
<img src='paket_usaha/$r[gambar]'></img>
<div class='harga-pkt'>$r[judul]</div>
<div id='container'>
<div class='expandable-panel' id='cp-$id_panel'>
<div class='expandable-panel-heading bold' style='background-color : $r[headcolor]; color : white;' >
<h2>klik disini untuk info paket<span class='icon-close-open'></span></h2>
</div>
<div class='expandable-panel-content' >
<p>$r[isi]</p>
</div>
</div>
</div>
";
$id++;}
}

?>
[/code]


here is the screenshot for the working full code : http://i.imgur.com/o1PogWI.jpg
Copy linkTweet thisAlerts:
@redflyshauthorJun 30.2016 — try alternating your quotes. One can't use single quotes inside single quotes![/QUOTE]

thank you... I tried that too but does not work

Might be simpler to not be in PHP mode until you absolutely need to be:
[code=php]
<?php
// assorted PHP code, then...
?>
<div class="paket_title" style="background-image: url('img1.jpg');">
<?php echo $r['head']; ?>
</div>
<?php
// more PHP code, perhaps...
[/code]


PS: The above would be assuming that img1.jpg is in the same directory as this file, in case that's the issue and you need to use a relative or absolute path to it, instead?[/QUOTE]


thank you for taking time answering this.

first of all this php is for generating an entire html page, so I need to write everything in PHP and echo it

the image is in the same directory with the php file

pleaase take a look at my full code :

[code=php]
<?php
// PAKET USAHA
if ($_GET['module']=='paketusaha'){
$paket = mysql_query("SELECT * FROM paket_usaha ORDER BY id_paket DESC LIMIT 5");
while ($r=mysql_fetch_array($paket)){
$id_panel=1;
$bg=$r['bgcolor'];
switch ($bg) {
case "red" :
echo "<div class='paket_title' style='background-image :url("img1.jpg"); '>" . $r['head'] . "</div>";
break;

case "blue" :
echo "<div class='paket_title' style='background-image :url("img1.jpg"); '>" . $r['head'] . "</div>";
break;

case "yellow" :
echo "<div class='paket_title' style='background-image :url("img1.jpg"); '>" . $r['head'] . "</div>";
break;


}


echo "
<img src='paket_usaha/$r[gambar]'></img>
<div class='harga-pkt'>$r[judul]</div>
<div id='container'>
<div class='expandable-panel' id='cp-$id_panel'>
<div class='expandable-panel-heading bold' style='background-color : $r[headcolor]; color : white;' >
<h2>klik disini untuk info paket<span class='icon-close-open'></span></h2>
</div>
<div class='expandable-panel-content' >
<p>$r[isi]</p>
</div>
</div>
</div>
";
$id++;}
}
?>
[/code]


here is the screenshot of my working code : http://i.imgur.com/o1PogWI.jpg
Copy linkTweet thisAlerts:
@RimtimJun 30.2016 — Another method create class of image and try.
Copy linkTweet thisAlerts:
@NogDogJun 30.2016 — If you do a "view source" of the current page output, what does that portion of the HTML look like? Is it what you expected, or is something structurally wrong with it, etc.? Something like the Firebug extension in Firefox can help to inspect it and see if it complains about anything.

PS: Not sure how critical it is, but the colon probably should be at the end of background-image, not at the beginning of url() :
<i>
</i>background-image: url('whatever')
Copy linkTweet thisAlerts:
@ginerjmJun 30.2016 — Your latest code sample is flawed. YOu are outputting the same image file for all the case statements. Assuming that you didn't mean to do that, look at this problem:
[code=php]
<img src='paket_usaha/$r[gambar]'></img>
[/code]

Any index needs to be quoted to make it clear what the index value is supposed to be. Your use of "gambar" without quotes forces php to look for a constant by that name first. Your lines should always be:
[code=php]
<img src='paket_usaha/$r["gambar"]'></img>
[/code]

and perhaps the reference to the array element needs to be wrapped in curly braces as well.

You might also read up on the use of heredocs. It makes writing large blocks of html that includes php vars much easier.
Copy linkTweet thisAlerts:
@NogDogJun 30.2016 — FWIW, I'd probably do it something more like this in order to avoid repetition (keep it D.R.Y. ? ):
[code=php]
if ($_GET['module']=='paketusaha'){
$paket = mysql_query("SELECT * FROM paket_usaha ORDER BY id_paket DESC LIMIT 5");
$bg_img = array( // assuming different image for each case?
'red' => 'img1.jpg',
'blue' => 'img2.jpg',
'yellow' => 'img3.jpg'
);
while ($r=mysql_fetch_array($paket)){
$id_panel=1;
// heredoc syntax makes quoting a bit less of an issue
echo <<<EOD
<div class="paket_title" style="background-image: url('{$bg_img[$r['bgcolor']]}')">
EOD;
// must be no leading white-space on preceding line
}
[/code]
Copy linkTweet thisAlerts:
@ginerjmJun 30.2016 — And last but more important - you really, really need to switch from using the MySQL_* functions to either the mysqlI_* ones or IMHO the PDO ones. If you have properly turned on php error checking (see my signature) during your devl cycle you should be seeing warnings about the removal of the MySQL interface and I'm sure you don't want your appl to suddenly breakdown when it does.
×

Success!

Help @redflysh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...