Quick PHP Question

Associate
Joined
26 Jun 2003
Posts
1,140
Location
North West
Im writing a class do do some mysql stuff then print out results in a table.

Is there anyway to print the resulting html code out in a well formatted way?

Code:
function test()
{
  $returnThis = "<table>".
                "  <thead>".
                "    <tr>".
                "      <th>Header 1/th>".
                "      <th>Header 2</th>".
                "      <th>Header 2</th>".
                "    </tr>".
                "  </thead>".
                "</table>";
  return $returnThis;
}

echo test();

However the above html prints it all out in one line. I know its no biggy but I like to see nice html code.
 
Associate
Joined
5 Jan 2003
Posts
752
Location
Norfolk
Im writing a class do do some mysql stuff then print out results in a table.

Is there anyway to print the resulting html code out in a well formatted way?


However the above html prints it all out in one line. I know its no biggy but I like to see nice html code.

Add a newline "\n" ;)

Code:
function test()
{
  $returnThis = "<table>\n".
                "  <thead>\n".
                "    <tr>\n".
                "      <th>Header 1/th>\n".
                "      <th>Header 2</th>\n".
                "      <th>Header 2</th>\n".
                "    </tr>\n".
                "  </thead>\n".
                "</table>\n";
  return $returnThis;
}

echo test();
 
Associate
Joined
21 May 2003
Posts
1,365
Indentation is just to keep it readable when you're writing it - if you're using PHP to generate the html, then why worry about how it's indented?
 
Associate
OP
Joined
26 Jun 2003
Posts
1,140
Location
North West
Haha thats a way i didnt think of. lol Cheers.

However tho, if you use the code like this:

Code:
...
  <body>
    <div id="wrapper">
      <?=$myClass->getTable()?>
    </div>
  </body>
...

Is printed out

Code:
  <body>
    <div id="wrapper">
      <table>
  <thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
      <th>Header 2</th>
    </tr>
  </thead>
</table>
    </div>
  </body>

As you can see it is tidyier, however only the opening <table> tag is indented properly.

Any way this can be sorted? :D

Sorry im picky :p
 
Soldato
Joined
11 May 2004
Posts
4,790
Location
Gloucester
The way I tend to do it is to run it through the Tidy PHP extension before displaying when developing / debugging my html / javascript. Then just outputting it normally when in production.
 

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
can I ask why you're concatenating those strings? seems rather pointless to me.
Code:
function test()
{
  return 
"<table>
  <thead>
    <tr>
      <th>Header 1/th>
      <th>Header 2</th>
      <th>Header 2</th>
    </tr>
  </thead>
</table>";
}

echo test();

needing your HTML indented like this makes for messy, confusing code in the long run. It's very worthwhile reading into generating HTML with the DOM in PHP.
 
Associate
Joined
21 May 2003
Posts
1,365
just use tidy tidy if you are such a perfectionist with the way your code looks (like me). worth the extra overhead in most cases imo :)

If you're running a high-traffic site then you'd be better off with no whitespace at all to save on bandwidth. If you want to read the code, just paste it into an editor with a html source format function.
 
Back
Top Bottom