Jump to content
Larry Ullman's Book Forums

Making Html Source Created Via Php Look Nice


Recommended Posts

This may sound like a silly request, but I'm using PHP to generate a whole bunch o' HTML based on MySQL data.

 

When I produce the HTML with the PHP, it looks fine on the screen, but if you look at the source, it's all bunched together, and really hard to read.

 

If possible, I'd like to clean this up, so it looks something like the sample below. Any suggestions? Thank you.

 

  <body>

   <div>

     <h1>Important title here</h1>

     <p>The above title is important because...</p>

   </div>

 </body>

Link to comment
Share on other sites

Breaking in and out of PHP like a WordPress template would do the trick I think - so:

 

<?php

   // Run query here
   // Get results into $row

?>

<body>

   <div>

     <h1><?php echo $row['title']; ?></h1>

     <p><?php echo $row['body']; ?></p>

   </div>

 </body>

 

The alternative is to echo out new line characters \n (inside "" not '') and tabs \t but I don't think you'd ever be able to get the indentation correct inline with your logic loops. Thats the only solution I know...

 

Hope that helps

 

Stuart Bates

  • Upvote 1
Link to comment
Share on other sites

There's nothing wrong with breaking out of PHP tags to print your surrounding HTML? Also another elegent solution would be to use heredocs:

 

<?php

echo <<<EOT;

<p>
    <b>Hello world!</b>
</p>

EOT;

?>

 

Might want to read the PHP manual or Larry's PHP5 Advanced book as the syntax is quite particular.

  • Upvote 1
Link to comment
Share on other sites

Well, perhaps, but that doesn't work for me. I have essentially created a WYSIWYG editor, and I do a search and replace on double newlines with p tags, etc. Basically, everything is together in one string, and the amount of parsing it would take to achieve that isn't worth it. I think I have found another solution though.

 

Will report back if I get it working.

Link to comment
Share on other sites

 Share

×
×
  • Create New...