Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post

    How do you indent HTML output by PHP?

    I'm querying a database, and printing results in a table. I'm the kind of person that likes to have tr indented 2 spaces from the table tag, and td indented 2 spaces from tr. What I'm doing right now is:

    PHP Code:
    echo "<table>";
    echo 
    "  <tr>";
    echo 
    "    <td></td>";
    echo 
    "  </tr>";
    echo 
    "  <tr>";
    echo 
    "    <td></td>";
    echo 
    "  </tr>";
    ...
    echo 
    "</table>"
    When I do it this way, I get the look I want (in the source).

    Is there a way to have PHP indent HTML automatically, rather than me adding the spaces myself?

  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    print "\n"; //creates new line
    print "\t"; //creates tab character

    so...
    print "<tr>";
    print "\n\t<td>";
    print "\n\t</td>";
    print "\n</tr>"

    should do it

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There is also HTML Tidy for PHP, but that may be going too far for your needs, http://www.coggeshall.org/tidy.php

    On my website I don't care about indenting HTML at all. If you look at the source code of this webpage you will see there are not even line breaks (well, only a bit as required by JavaScript), http://skyscraperpage.com/members/

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    Originally posted by Dylan Leblanc
    There is also HTML Tidy for PHP, but that may be going too far for your needs, http://www.coggeshall.org/tidy.php
    Thanks, but I think that's going too far for my needs.

    Yikes, that's a mess!

    bcarl, I'll try your suggestion.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    All right, the new line character is helpful (so every HTML tag in the output source is on it's own line). When I used the tab character, it indented by 8 spaces...I prefer 2 space indents in my HTML documents, so that's no good. So I'm more or less back to where I was before. But that's okay, since I'm not having to do that indenting too often.

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think the 8 spaces is a result of the program you are using to view the html document, since tabs are sometimes different widths in different programs. Sometimes there are even settings in the program which you can change the width of the tabs.

    I made my webpages without any newlines because html documents don't need them.

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    On my website I don't care about indenting HTML at all. If you look at the source code of this webpage you will see there are not even line breaks
    I'll second that. I've never had any need to bother with my xhtml-sources layout. (But then again, i never make mistakes so i don't need to look at the xhtml-source when debugging )

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    But in the interests of visibility and readability for bothmyself if i want to change something, and other people who are using my html - i want it to be laid out neatly and structured using a bit of whitespace.
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #9
    New Coder
    Join Date
    Sep 2003
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    highlight_string($string);

    It keeps spaces and highlights php etc

  • #10
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Originally posted by Grant Palin
    All right, the new line character is helpful (so every HTML tag in the output source is on it's own line). When I used the tab character, it indented by 8 spaces...I prefer 2 space indents in my HTML documents, so that's no good. So I'm more or less back to where I was before. But that's okay, since I'm not having to do that indenting too often.
    The number of spaces displayed is directly related to your text editor. Check the configuration of your text editor and set the spaces per tab (or something similar) to 2.

    Alternativly, I think you can use \s for a space character. Not sure though

  • #11
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes, you can

  • #12
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    or look at herdoc syntax for wysiwyg

    PHP Code:
    <?php

    $str 
    = <<<TABLE
    <table>
        <tr>
            <td>$variable</td>
        </tr><tr>
            <td>
    {$_SESSION['another_var']}</td>
        </tr>
    </table>
    TABLE;

    echo 
    $str;

    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #13
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    as i've been using it recently, the normal quotes syntax seems to support newlines as well

    so

    echo 'hello
    ';
    echo "hello
    ";
    echo "hello\n";


    are all equal.
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •