Jump to content

Echoing html code


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
https://forums.phpfreaks.com/topic/287440-echoing-html-code/
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
?>
Link to comment
https://forums.phpfreaks.com/topic/287440-echoing-html-code/#findComment-1474606
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;
?>
Link to comment
https://forums.phpfreaks.com/topic/287440-echoing-html-code/#findComment-1474607
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
https://forums.phpfreaks.com/topic/287440-echoing-html-code/#findComment-1474641
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.