Getting PHP generated HTML to display properly.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Getting PHP generated HTML to display properly.

Post by TonsOfFun »

When I use PHP to display HTML, it always shows up on a single line when I view page source.
It makes it much more difficult to debug as well as making it look unprofessional.

How can I get it to display on different lines?
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Getting PHP generated HTML to display properly.

Post by s992 »

You need to add all the standard HTML formatting tags, <p>, <br />, etc.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Getting PHP generated HTML to display properly.

Post by Pazuzu156 »

This sounds weird. Post a snippet of code you are having trouble with. Also, when you view the page source from the browser, the php code will not show up, only the html the php is created will show up.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: Getting PHP generated HTML to display properly.

Post by TonsOfFun »

Pazuzu156 wrote:This sounds weird. Post a snippet of code you are having trouble with. Also, when you view the page source from the browser, the php code will not show up, only the html the php is created will show up.

This is a snippet:

Code: Select all

<div id="text_body">
           	<div id="left_menu"><p><a href="album.php" onclick="window.open('album.php?action=add&album_id=','Add','width=410,height=480')">Add Album</a></p></div><div id="main"><table><tr><td><strong>Album Name</strong></td><td><strong>Description</strong></td><td><strong>Date Created / Last Update</strong></td><td></td></tr></table></div><div id="clear"></div>		</div>

                <div id="text_bottom">
                	<div id="text_bottom_left"></div>
                    <div id="text_bottom_right"></div>
                </div>
This is the code generated by my PHP code:
In View Page Source it is all on one line

Code: Select all

<div id="text_body">
           	<div id="left_menu"><p><a href="album.php" onclick="window.open('album.php?action=add&album_id=','Add','width=410,height=480')">Add Album</a></p></div><div id="main"><table><tr><td><strong>Album Name</strong></td><td><strong>Description</strong></td><td><strong>Date Created / Last Update</strong></td><td></td></tr></table></div><div id="clear"></div>		</div>
This is coded outside PHP:
It is displayed on separate lines

Code: Select all

           	<div id="text_bottom">
                	<div id="text_bottom_left"></div>
                    <div id="text_bottom_right"></div>
                </div>
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Getting PHP generated HTML to display properly.

Post by Pazuzu156 »

Okay, I understand what you're getting at. As I see it, when you go to debug, the page source from the browser isn't the best idea. What text editor are you using? And what operating system?
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: Getting PHP generated HTML to display properly.

Post by TonsOfFun »

Pazuzu156 wrote:Okay, I understand what you're getting at. As I see it, when you go to debug, the page source from the browser isn't the best idea. What text editor are you using? And what operating system?
Dreamweaver CS5
Windows 7
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Getting PHP generated HTML to display properly.

Post by Pazuzu156 »

Ok, Dreamweaver is good for web design, I cannot say it's good for php as sometimes php wont even show up on the pages. Use notepad++, its a free open source code editor for all types of coding needs. It has line numbers and is excellent for hand coding. Also, Eclipse has a program just for php and if I'm correct will parse your code as well. Check them out.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: Getting PHP generated HTML to display properly.

Post by TonsOfFun »

Pazuzu156 wrote:Ok, Dreamweaver is good for web design, I cannot say it's good for php as sometimes php wont even show up on the pages. Use notepad++, its a free open source code editor for all types of coding needs. It has line numbers and is excellent for hand coding. Also, Eclipse has a program just for php and if I'm correct will parse your code as well. Check them out.
I've tried notepad++ but I like how dreamweaver manages sites.
But I'll try to get into Notepad++ and check out Eclipse.

Another fear I have is that if someone, looks at my code, it will look sloppy and unprofessional.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Getting PHP generated HTML to display properly.

Post by Pazuzu156 »

That is a possibility, If you look at most professional websites, alot of the codes can look that way.
With my code, i used a mix, have a look:

Code: Select all

<html>
<body>
<?php
function spamcheck($field)
  {
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    } else {
    return FALSE;
    }
  }

if (isset($_REQUEST['email']))
  {
  $mailcheck = spamcheck($_REQUEST['email']);
  if ($mailcheck==FALSE)
    {
    echo "Invalid input";
    } else {
    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail("kklein_webmaster@jae-entertainment.we.bs", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
    }
  } else {
  echo "<form method='post' action='email.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html>
And I ended up with this as the source, aligned in good order.

Code: Select all

<html> 
<body> 
<form method='post' action='email.php'> 
  Email: <input name='email' type='text' /><br /> 
  Subject: <input name='subject' type='text' /><br /> 
  Message:<br /> 
  <textarea name='message' rows='15' cols='40'> 
  </textarea><br /> 
  <input type='submit' /> 
  </form> 
</body> 
</html> 
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: Getting PHP generated HTML to display properly.

Post by TonsOfFun »

I figured it out.

I was putting each line of HTML in seperate echo() statements.
But when I out it all in one, it worked.

Thanks Pazuzu156 for the help and tips on the text editors.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Getting PHP generated HTML to display properly.

Post by Pazuzu156 »

No problem at all, when you post them into separate echo() commands, the computer reads it without the professional look. So it will parse the codes and show in a single line. Also, alot is shown in multiple echo() codes so depending on what you have echo in will determine whether it shows in one line or multiple.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Getting PHP generated HTML to display properly.

Post by Pazuzu156 »

I have a solution to your issue. When you post things into separate echos, if you want a more professional look, at the end of the echo, make a new line. When you make a new line, you use \n. it would look like so:

Code: Select all

<?php

echo "Hello\n";
echo "World!\n";
echo "Such a lovely day huh?";

?>
In the browser, your page will look like this:

Code: Select all

Hello World!
Such a lovely day huh?
The page source will look like this:

Code: Select all

<html>
<body>
Hello
World!<br />
Such a lovely day huh?
</body>
</html>
Hope this helps.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply