Page 1 of 1

Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 7:43 pm
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?

Re: Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 9:13 pm
by s992
You need to add all the standard HTML formatting tags, <p>, <br />, etc.

Re: Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 9:57 pm
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.

Re: Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 10:11 pm
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>

Re: Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 10:17 pm
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?

Re: Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 10:49 pm
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

Re: Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 10:56 pm
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.

Re: Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 11:04 pm
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.

Re: Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 11:12 pm
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> 

Re: Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 11:20 pm
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.

Re: Getting PHP generated HTML to display properly.

Posted: Mon Nov 22, 2010 11:27 pm
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.

Re: Getting PHP generated HTML to display properly.

Posted: Thu Nov 25, 2010 1:22 pm
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.