Jump to content

Echoing html code


Go to solution Solved by QuickOldCar,

Recommended Posts

Hello. I'm new to PHP and have kinda just been playing around. But I ran into a problem. I want to echo this:

    echo 
                     '<div class=""list"" onclick=""location.href="#""">
                <div class=""serverListHover"" style=""width: 90%; float: right"">
                    <div class=""serverListHover"" style=""width: 25%; float: right"">
                        <span style=""font-size: 12px;"">Ip Address Here</span>
                        <div style=""background:blue; height:4px""></div>
                    </div>
                    <div class=""serverListHover"" style=""width: 75%; float: left"">
                        <img class=""listImage"" src=""http://www.news.balkanenergy.com/wp-content/uploads/2014/02/InEnerg14-460x90-EN.jpg"" alt=""Pulpit rock"" width=""460"" height=""60"">
                    </div>
                </div>
                <div class=""serverListHoverLeft"">
                    <span class=""rank"" style=""display:inline-block; vertical-align:middle"">123</span>
                </div>
            </div>';

But it won't turn out right. First off, the styles/classes don't seem to be working. All it shows with this code is "IP Address Here" and "123". No styles at all. And secondly, is this a good way to do this? I plan on using a MySQL database to pull data from and insert into parts of this code, but I want to know it this will be efficient enough(or if there's a easier/better way).

 

Thanks,

HeyAwesomePeople

Link to comment
Share on other sites

Wrap everything in double quotes and single quotes within.
 
Or use heredoc
 
<?php
echo <<<"MYHTML"
<div class=""list"" onclick=""location.href="#""">
<div class=""serverListHover"" style=""width: 90%; float: right"">
<div class=""serverListHover"" style=""width: 25%; float: right"">
<span style=""font-size: 12px;"">Ip Address Here</span>
<div style=""background:blue; height:4px""></div>
</div>
<div class=""serverListHover"" style=""width: 75%; float: left"">
<img class=""listImage"" src=""http://www.news.balkanenergy.com/wp-content/uploads/2014/02/InEnerg14-460x90-EN.jpg"" alt=""Pulpit rock"" width=""460"" height=""60"">
</div>
</div>
<div class=""serverListHoverLeft"">
<span class=""rank"" style=""display:inline-block; vertical-align:middle"">123</span>
</div>
</div>
MYHTML;
?> 

Forgot to mention can escape in and out of php to hml as well

<?php
//some php code here
?>
<div class=""list"" onclick=""location.href="#""">
<div class=""serverListHover"" style=""width: 90%; float: right"">
<div class=""serverListHover"" style=""width: 25%; float: right"">
<span style=""font-size: 12px;"">Ip Address Here</span>
<div style=""background:blue; height:4px""></div>
</div>
<div class=""serverListHover"" style=""width: 75%; float: left"">
<img class=""listImage"" src=""http://www.news.balkanenergy.com/wp-content/uploads/2014/02/InEnerg14-460x90-EN.jpg"" alt=""Pulpit rock"" width=""460"" height=""60"">
</div>
</div>
<div class=""serverListHoverLeft"">
<span class=""rank"" style=""display:inline-block; vertical-align:middle"">123</span>
</div>
</div>

<?php
//more php code
?>
Edited by QuickOldCar
Link to comment
Share on other sites

  • Solution

This one should work

 

 

<?php
echo <<<MYHTML
<div class="list" onclick="" location.href="#">
<div class="serverListHover" style="width: 90%; float: right">
<div class="serverListHover" style="width: 25%; float: right">
<span style="font-size: 12px;">Ip Address Here</span>
<div style="background:blue; height:4px"></div>
</div>
<div class="serverListHover" style="width: 75%; float: left">
<img class="listImage" src="http://www.news.balkanenergy.com/wp-content/uploads/2014/02/InEnerg14-460x90-EN.jpg" alt="Pulpit rock" width="460" height="60">
</div>
</div>
<div class="serverListHoverLeft">
<span class="rank" style="display:inline-block; vertical-align:middle">123</span>
</div>
</div>
MYHTML;
?>
Link to comment
Share on other sites

 

This one should work

<?php
echo <<<MYHTML
<div class="list" onclick="" location.href="#">
<div class="serverListHover" style="width: 90%; float: right">
<div class="serverListHover" style="width: 25%; float: right">
<span style="font-size: 12px;">Ip Address Here</span>
<div style="background:blue; height:4px"></div>
</div>
<div class="serverListHover" style="width: 75%; float: left">
<img class="listImage" src="http://www.news.balkanenergy.com/wp-content/uploads/2014/02/InEnerg14-460x90-EN.jpg" alt="Pulpit rock" width="460" height="60">
</div>
</div>
<div class="serverListHoverLeft">
<span class="rank" style="display:inline-block; vertical-align:middle">123</span>
</div>
</div>
MYHTML;
?>

Works, thanks!

 

 

@HeyAwesomePeople your code is fine, the problem is you seem to be wrapping your HTML attributes in two or three sets of double quotes eg   attr=""value""  . Attribute values should only be wrapped in one set of double quotes eg   attr="value"

I tried that, but it didn't work. I think it's because I am trying to put values from a MySQL server in it as well. But the above one showed works fine and is a little less confusing. Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.