HartleySan Posted March 19, 2011 Share Posted March 19, 2011 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 More sharing options...
Stuart Posted March 19, 2011 Share Posted March 19, 2011 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 1 Link to comment Share on other sites More sharing options...
HartleySan Posted March 19, 2011 Author Share Posted March 19, 2011 Yeah, that's what I was afraid of. I could also use or enter extra spaces and run them through some sort of entities function. Oh well. Link to comment Share on other sites More sharing options...
Stuart Posted March 19, 2011 Share Posted March 19, 2011 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. 1 Link to comment Share on other sites More sharing options...
HartleySan Posted March 20, 2011 Author Share Posted March 20, 2011 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 More sharing options...
Larry Posted March 21, 2011 Share Posted March 21, 2011 You can use \t to create tabs in the HTML source, just as you use \n to create newlines. Or you could put the formatting into the PHP code (by using tabs and newlines within the echo statement). Link to comment Share on other sites More sharing options...
HartleySan Posted March 22, 2011 Author Share Posted March 22, 2011 Yeah, and that's what I'll probably end up doing to space things out a bit. I'm really picky about this sort of thing though, and I wanted to add spaces, not tabs, but I suppose I can't always have my cake and eat it too. Link to comment Share on other sites More sharing options...
Recommended Posts