Results 1 to 4 of 4

Thread: Formatting HTML code using PHP

  1. #1
    Join Date
    Feb 2013
    Location
    California
    Posts
    86
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Formatting HTML code using PHP

    I assume this is a very basic question but I am not able to find a solution.

    I have used PHP for the past several years and am mostly proficient in its use. My problem is that when I "view source" the html created from the PHP is not structured/formatted like stand-alone HTML (i.e., long lines with no returns). What must one do in the PHP code to cause the resultant PHP created HTML code to appear normal.

    TIA for any assistance
    jdadwilson

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Is this what you mean?
    Code:
    <?php
    
    echo "<table>
    	<tr>
    		<td>Hello</td>
    	</tr>
    </table>\n";
    
    ?>
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Feb 2013
    Location
    California
    Posts
    86
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default

    Somewhat, but not exactly...

    My coding is generally to let PHP do its thing but then convert back to HTML for the output rather than using echos for the PHP. Example...

    Code:
    <div class="Letters" id="Table-<?php echo $letter;?>" style="display:none;">
    	<h3>Family Pages for letter - <?php echo $letter;?></h3>
    	<ul class="list_links"><?php							
    		while ($rec_Fam = $query_PDO->fetch(PDO::FETCH_ASSOC)) {?>
    			<li><a href="familypages.php?fam_ltr=<?php 
    				echo $letter;?>&amp;fam_ID=<?php 
    				echo $rec_Fam['fam_id'];?>"><?php 
    				echo $rec_Fam['fam_name_short'];?></a>
    			</li><?php
    		}?>
    	</ul>
    </div><?php
    The above code generates multiple line of HTML but with no structure. (see the source on this link: http://www.txfannin.org/new-site/familypages.php. Specifically note the <li> code.).
    It seems that even straight HTML code which is interrupted with PHP gets messed up. (note div for container-fluid followed by div for panel [looks good] but then the div for the page-header [which is preceded by PHP code] is no longer formatted properly).

    Is it as simple as adding an echo "\n"; after selected changes to PHP (e.g., </li><?php echo "\n"?

    Thanks for your assistance
    jdadwilson

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    You can echo tabs and new lines to create the indented code (see my previous demo for an example for reference). It's easier to manipulate if you put the echo at the start of the line an then add the tabs ahead of whatever variable you're echoing (again, see how the demo has the echo right up to the left, followed by tabs, before the HTML).

    \n is like a new line - it's an escape character with special meaning so it must be enclosed in double-quotes for it to become a line feed.

    Put it in single-quotes and it will be interpreted as a literal string instead. More info along with other special escape characters here http://php.net/manual/en/language.types.string.php
    Last edited by Beverleyh; 11-03-2015 at 08:40 PM.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

Similar Threads

  1. Help With HTML CODE!
    By bbilal in forum HTML
    Replies: 16
    Last Post: 06-16-2011, 07:50 PM
  2. Replies: 3
    Last Post: 05-12-2011, 03:43 AM
  3. Replies: 15
    Last Post: 06-11-2009, 12:27 PM
  4. Replies: 2
    Last Post: 10-27-2008, 05:16 AM

Bookmarks

Posting Permissions

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