Forum Moderators: coopster

Message Too Old, No Replies

PHP Tidy

         

andrewheiss

5:22 pm on Feb 18, 2008 (gmt 0)

10+ Year Member



I have an application that show information from a MySQL DB in a table created by a PHP while statement - pretty standard stuff, as partially shown below:


echo("<td>$person[address]</td>");
echo("<td>$person[city]</td>");
echo("<td>$person[state]</td>");
echo("<td>$person[zipCode]</td>");
echo("<td>$person[phone]</td>");

The page works and renders just fine. The only problem is the source code; when I view the source, the entire table is put on one looong line.

I've heard that the PHP Tidy extension will clean up outputted PHP rendered HTML, but I can't figure out how to use it. I've found a way to render another page:


$tidy = new tidy("input.html");
$tidy->cleanRepair();
echo $tidy;

But that would require tidying up another page, meaning I'd need to have two separate PHP files for one page. Is there any way to put the tidy statement on the same page rather than tidying some other page?

How can I make it actually clean up my messy table?

Thanks!

cameraman

5:38 pm on Feb 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can make it tidy yourself by adding newlines to your echos:
echo("<td>$person[address]</td>\n");

To use tidy, you have to use output buffering. If you go to the tidy manual [webmasterworld.com] page and scroll to example #2 (a long way down) you can see how to do it - you'll be surrounding your output, as if all of your output is between <html> and </html> in the example.

PHP_Chimp

10:45 pm on Feb 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not that this helps with your tidy issue, however it does help with not having to type \n every time you want a new line.

To save writing echo lots of times you can also use heredoc's -


echo("<td>$person[address]</td>");
echo("<td>$person[city]</td>");
echo("<td>$person[state]</td>");
echo("<td>$person[zipCode]</td>");
echo("<td>$person[phone]</td>");
// becomes
echo <<<STUFF
<td>$person[address]</td>
<td>$person[city]</td>
<td>$person[state]</td>
<td>$person[zipCode]</td>
<td>$person[phone]</td>
STUFF;

The code is displayed including any line breaks that you put in there.
From that manual [uk.php.net] -
echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;