PHP echo inline CSS background url - php

I am trying to echo some inline CSS using PHP using this:
echo '<div class="image" style="background:url("img/testimage.jpg");width:300px;height:232px;">';
echo '</div>';
But for some reason this is returning this:
<div class="image" testimage.jpg");width:300px;height:232px;"="" img="" style="background:url("></div>
This is within a WordPress environment, am I doing something obvious wrong?

Escape the quotes inside the url declaration properly:
echo '<div class="image" style="background:url(\'img/testimage.jpg\'); width:300px; height:232px;">';
// ^ ^

you can't run something like this (in HTML) correctly:
style="background:url("img/testimage.jpg");width:300px;height:232px;"
must merge between single and duple quotes or escape them:
style="background:url('img/testimage.jpg');width:300px;height:232px;"
solution:
echo "<div class='image' style='background:url(\"img/testimage.jpg\");width:300px;height:232px;'></div>";

echo '<div class="image" style="background:url(\'img/testimage.jpg\');width:300px;height:232px;">';
echo '</div>';

Related

Escape quotes on onclick metho

I have click method generated by php echo. It does not render as it should be. It shows as in the attached image.
my code is
echo "<div class='col-sm-3' onclick='ViewItem('".$item['item_id']."')' style='cursor:pointer'>";
how can I escape the quotes to get the following
<div class='col-sm-3' onclick='ViewItem("1")' style='cursor:pointer'>
We have all been there looking at code too long.
$item['item_id'] = '6';
echo "<div class='col-sm-3' onclick='ViewItem(\"".$item['item_id']."\")' style='cursor:pointer'>";
Output:
<div class='col-sm-3' onclick='ViewItem("6")' style='cursor:pointer'>

Getting string for database and use it as img src

I have problem looping out pictures from a database.
I have no problem getting the information(string) that I need from the database. The problems accure when I try to use the string as a img src-tag.
My code:
<?php
foreach ($console as $con):
echo '<li>', $con['brand'], ', ',$con['pic'], '</li>';
echo'<div class="item2">
<a href="xboxconsol.html">
<img src=',$con['pic'],'/>
</a>
</div>';
endforeach;
?>
Note, the li-elements put out the correct information but when I use it in the img-tag it adds a "/" at the end on the string.
Example of list output:
Playstation, ../playstation.jpg
The picutre however output the URL to the pic as:
../playstation.jpg/
Where do the last "/" come from and how do I get rid of it?
You need to quote the attribute value.
echo'<div class="item2">
<a href="xboxconsol.html">
<img src="',$con['pic'],'"/>
</a>
</div>';
Currently the browser is guessing what the attribute should contain.
Which comes out as:
<img src="value/">
but you want:
<img src="value"/>
You need to quote your concatenations, be very careful how you output HTML from PHP.
I'd rather concatenate the variables like this instead:
foreach ($console as $con)
{
echo "<li>{$con['brand']} {$con['pic']}</li>";
echo "<div class='item2'>
<a href='xboxconsol.html'>
<img src=\"{$con['pic']}\" />
</a>
</div>";
}
It's way cleaner and more readable.

How to escape quotes of array keys inside background-image url?

This is my code :
echo '<div class="banner" style="background-image: url(<?php echo $img_Array["sizes"]["large"]; ?>);">';
If I do
echo $img_Array["sizes"]["large"];
I get correct image path but when I use it inside background-image it does not worked for me.
When I used inspect element it displayed:
element.style {
background-image: url(<?php echo $img_Array[;
}
That means it needs to escape quotes(") of sizes , i tried to use /" but didnt work .
Any help would be appreciated . Thank you .
Try this..
echo '<div class="banner" style="background-image: url(' . $img_Array["sizes"]["large"] . ' );">';
When you echo, you are already in PHP. No need to use <?php again.
echo '<div class="banner" style="background-image: url(<?php echo $img_Array["sizes"]["large"]; ?>);">';
Change it to:
echo '<div class="banner" style="background-image: url(' . $img_Array["sizes"]["large"] . ');">';
It seems like you've tried to nest PHP code blocks. There are 2 possible solutions:
Echo the entire HTML code from the PHP block:
<?php
echo '<div class="banner" style="background-image: url('
. $img_Array["sizes"]["large"] . ');">';`
?>
Echo just the URL with a PHP block nested in HTML:
<div class="banner" style="background-image: url('<?php echo $img_Array["sizes"]["large"]; ?>');">

How can I make this line work, and what's the science behind these quotation marks?

I really need to get the following line working and apart from that, would love to read an article or anything of that form to finally know how to use my quotation marks properly.
echo '<div class="video"><iframe width="540" height="330" src="'//www.youtube.com/embed/ . $row['link']"/>'</frame>;
Should be good :
echo '<div class="video"><iframe width="540" height="330" src="//youtube.com/embed/'. $row['link'] .'"></iframe></div>';
But read comments, your error was pretty easy to find with highlights syntax.
The quotes are not placed properly.
Try this:
<?php
$link='//www.youtube.com/embed/'. $row['link'];
?>
<div class="video">
<iframe width="540" height="330" src="<?php echo '//www.youtube.com/embed/'. $row['link']; ?>" /></iframe>
</div>

Nesting HTML/PHP statement inside another PHP statement

I have an IF/ELSE statement and I would like to print out some images that I am getting from my Drupal site. I can't figure out how to print those IMG tags without getting errors.
This is what I have so far:
<?php
$field = field_get_items('node', $node, 'field_visitor_image');
if($field){
<img src="<?php print image_style_url('lead_teaser', $node->field_visitor_image['und'][0]['uri']); ?>">
}
else
{
<img src="<?php print image_style_url('lead_teaser', $node->field_banner_image['und'][0]['uri']); ?>">
}
?>
You have to break out of PHP mode when you start outputting HTML.
if($field){
?>
<img src="<?php print image_style_url('lead_teaser', $node->field_visitor_image['und'][0]['uri']); ?>">
<?php
}
Use echo and string concatenation:
if ($field) {
echo '<img src="' . image_style_url('lead_teaser', $node->field_visitor_image['und'][0]['uri']) . '">';
}
You cannot nest
<?php > inside another <?php >.
One option for you could be to concatenate using ".".

Categories