Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> [php] Clean Code Functions, Clean up your html output from php scripts
jlhaslip
post Feb 21 2008, 03:41 AM
Post #1


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,882
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



There is another Topic about writing 'clean' HTML code posted elsewhere on the Forum. I'll edit this Topic and add the link so you can review it on your own, and there is no need for me to comment on it in this thread, but the purpose of this Topic is to introduce a pair of functions which can be used for making sure that the HTML output from my scripts is readable when a view-source is reviewed.

Two handy functions are included here. They work together quite nicely, and I will start this Tutorial with a short summary of the reasons for their 'being'.

Purpose Explained:

For the most part, Web Designers use php as a tool for creating 'dynamic content' on their Web sites. PHP is used to generate the code for a Web Site in HTML (or xml, javascript, etc.). Essentially, the script send off pieces of 'text' which a Browser 'interprets' onto the screen. te text which is sent is (x)HTML, for all intents and purposes. Quite often, the HTML code will send out "<br />" tags for adding a new line of text on the Browser BUT the new line is displayed on the screen only, not in the html code itself. In other words, the source code lists the <br /> tags but the source code is not 'formatted' when you do a view source of the page. So... as a tool for cleaning up the display of the HTML code on a view-source, I often include the following two functions into my scripts to do exactly that.

The first one is for issuing Tabs for indenting blocks of code. The second one injects a new-line control character for a new line.
QUOTE

function tabs($t=1){ // to print $t number of tab characters
for($x = 1; $x <= $t; $x++){
$output .= "\t"; // back-slash t is a tab control character
}
return $output;
}
QUOTE

function nls($n=1){ // to print $n number of new-lines
for($x = 1; $x <= $n; $x++){
$output .= "\n"; // back-slash n is a new-line control character ( \n\r on some systems... Linux or Macs?)
}
return $output;
}

Explanation of the Functions:

The function " tabs" is defined with a default parameter value of "1" which is the default number of tab characters you will include as you use the function. To produce a tabs character in your HTML output, simply call this function in your script as "echo'd" or printed output. the default value is one, but including an integer value as a passed parameter for the function will increase the number of tabs or new-lines in your HTML output.

Examples:
CODE
echo tabs(); // will provide a single tab character
echo tabs(4); // will provide 4 tab characters

Similarly, you can have your HTML output issue a new line by using the
CODE
echo tabs(); // will provide a single new-line
echo tabs(4); // will provide 4 new-lines


Usage:

Here is a sample of a using these functions in a php script to write a readable table :
CODE
<?php

echo nls()'<table>'; // start the table tag on a new line
echo nls(),tabs(1) . '<tr>'; // start the table row tag on a new line with a single tab
echo nls(),tabs(2) . '<td>iterations</td>'; //
echo nls(),tabs(2) . '<td>Interval</td>';   // start the table data tags on a new line with two tabs
echo nls(),tabs(2) . '<td>Average</td>';  //
echo nls(),tabs(2) . '<td>Wait</td>';        //
echo nls(), tabs(1) . '</tr>'; // start the table row end tag on a new line with a single tab
echo '</table>'; // start the table end tag on a new line
echo nls();

?>


I can not emphasis enough that the tabs characters and new lines will affect the view-source output only, not the actual output in your page.

Play with it a little.

*** Note: the tabs characters should be issued AFTER any newlines, or the tabs are effectively added to the previous lines and to not Indent the code in the view-source output properly.
*** Note: HtmlTidy is a script available as an add-on that does an very nice job of formatting the view-source, also. check it out at the Mozilla Extensions Site. It is also a part of the Firebug extension. (I think)

Hope this helps someone, and post any comments you might have about this Tutorial.
Go to the top of the page
 
+Quote Post
galexcd
post Feb 21 2008, 05:07 AM
Post #2


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
*********

Group: [HOSTED]
Posts: 975
Joined: 25-September 05
From: The dungeon deep below the foundation of trap17
Member No.: 12,251



Ahh. What a well written tutorial. You've even got the default values for the parameters in the functions. A nice extra touch. I am not a very neat coder, but I may use this script somewhere on the net. But as some of you know I love short code, the shorter the better and if you don't mind I would like to provide a recursion example:

Tabs:
CODE
function tabs($t=1){ // to print $t number of tab characters
return ($t==1)?"\t":"\t".tabs($t-1);
}


Nls:
CODE
function nls($n=1){ // to print $n number of new-lines
return ($n==1)?"\n":"\n".nls($n-1);
}


This post has been edited by alex7h3pr0gr4m3r: Feb 21 2008, 05:08 AM
Go to the top of the page
 
+Quote Post
jlhaslip
post Feb 21 2008, 06:43 AM
Post #3


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,882
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



Great modification to that function.

thanks Alex...
Go to the top of the page
 
+Quote Post
rvalkass
post Feb 21 2008, 07:41 AM
Post #4


apt-get moo
Group Icon

Group: [MODERATOR]
Posts: 2,055
Joined: 28-May 05
From: Hertfordshire, England
Member No.: 7,593
Spam Patrol



QUOTE
$output .= "\n"; // back-slash n is a new-line control character ( \n\r on some systems... Linux or Macs?)


\n is the control character that represents a linefeed. On Linux and UNIX systems (and a wide array of others), the linefeed character is used to represent a new line. \r is the carriage return character, and is used by Windows in conjunction with the linefeed character to create a new line. The sequence to cause a new line on Windows is in fact \r\n in that order!

The reason \n works on Windows is because it gets translated 'behind the scenes' into the \r\n sequence.
Go to the top of the page
 
+Quote Post
sad1sm0
post Mar 18 2008, 07:06 PM
Post #5


Newbie
*

Group: Members
Posts: 8
Joined: 18-March 08
Member No.: 59,510



I have written myself a function that automatically adds line feeds, but i never thought to do the tabs. That's a good move.

My new line code looks like this


CODE
function writeln($line) {//obviously i stole this from javascript, but i use it in place of echo most of the time.
echo $line."\n";
}

Go to the top of the page
 
+Quote Post
fffanatics
post Mar 18 2008, 09:33 PM
Post #6


Privileged Member
*********

Group: [HOSTED]
Posts: 937
Joined: 14-April 05
From: West Chester, PA
Member No.: 5,636



Very nice tutorial. I have always done something similar but nothing this oop. This tutorial could definitely be expanded into a 'clean' html php class so that one just includes it and uses these functions from that class. I like the tutorial and probably will do something with it in the future.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. HTML tags and examples(37)
  2. Check Your Scripts(4)
  3. How To Put A Phpbb Login Box On Your Main Site.(18)
  4. Basic Html (for Us Kids)(23)
  5. Php Spy Code(21)
  6. How To Create Html Page Anchors(5)
  7. Your First Autoit(2)
  8. Fantastico Scripts - A 'how To'(0)
  9. Cpanel Preinstalled Scripts, Extras, And Cpanel Options(1)
  10. Integrating Html And Css Part 1(5)
  11. Fast Template Design In Joomla Cms(2)
  12. Document Type Declarations(0)
  13. Simple Scripts In Html And Javascript(7)
  14. How To Make A Simple File Based Shoutbox Using Php And Html(8)
  15. Ucfirst() And Ucword() Functions In Game Maker(2)
  1. Do You Want To Use Php Code In Your Html Pages?(9)
  2. Spice Up Your Forms(11)
  3. How To Install Php Scripts(0)
  4. Html Legend(9)
  5. Nice Clean Php Layout Coding.(7)
  6. Create A Simple Html Editor With Php And Javascript(3)
  7. Creating Navigation For Html Websites(12)
  8. Want-to-start Html Tutorials(2)
  9. Html Bdo...(13)
  10. Html Span(7)
  11. Cakephp On Ubuntu(0)
  12. How To: Html Tables.(8)
  13. Add Flashing Inbox To Invisionfree Forum(0)


 



- Lo-Fi Version Time is now: 26th July 2008 - 07:56 AM