0

Basically I want to create a div with specific styling using PHP.

I have the following intended style saved up in a separate string for easy editing:

$bg = "background: url('./flags/" . $country[$id[0]]["iso"] . ".png')no-repeat center center fixed;";

And this is the echo that generates the div:

echo "<div class='flag' style='" . $bg . "'></div>";

When I run this code, the div does appear, but the style part is all jumbled up and weirdly formatted, like so:

<div class="flag" style="background:url(\" .="" flags="" hk.png\')no-repeat="" center="" fixed;'=""></div>

What is causing this problem?

Thanks in advance.

10
  • Works fine for me! get different output! (Think you use other code as you show here, because you have a value inside the div tag!) Commented Dec 6, 2014 at 16:52
  • @Rizier123 Yes I was testing things and added that "sdf" thingy to see if it appeared, but it doesn't really interfere with anything else. I'll remove it to avoid confusion. Thanks! Commented Dec 6, 2014 at 16:56
  • Take a look here: ideone.com/1QQyQZ I don't get the same output with: \" or .="" Commented Dec 6, 2014 at 16:58
  • 1
    Is there a reason why you can't have the style for .flag saved in a separate CSS file? That might avoid the problem altogether. Commented Dec 6, 2014 at 17:01
  • 1
    Gotcha. I think it might be related to the fact that in the $bg variable, you're encasing the url in single quotes -- but in your echo statement, you're doing the same thing for the style attribute. So when your $bg variable renders, it's closing the single quotes. I could be wrong, but I would try switching out the quotes in your $bg variable like this: $bg = 'background: url("./flags ... etc ... Commented Dec 6, 2014 at 17:12

1 Answer 1

1

I guess I'll repost this here, since it worked out:

In the $bg variable, you're encasing the url in single quotes -- but in your echo statement, you're doing the same thing for the style attribute. So when your $bg variable renders, it's closing the single quotes. I could be wrong, but I would try switching out the quotes in your $bg variable like this:

$bg = 'background: url("./flags ... etc ...
Sign up to request clarification or add additional context in comments.

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.