Jump to content
xisto Community

vujsa

Members
  • Content Count

    1,008
  • Joined

  • Last visited

Everything posted by vujsa

  1. Wow, I figured that that would be a fairly common function. Glad I could have pass along the knowledge.The code generated is actually two hex strings and a digit. (13 places)Guess that's why we're here! vujsa
  2. Check out: Add Favicon To Your Site, Creating, Converting to icon, Setupmastercomputers wrote this very good tutorial.Happy Learnin', vujsa
  3. Hey M^E, Nice suggestion for creating unique file names, I used to use a sepperate count file that I incremented after each new file name. PHP's uniqid() function does the same thing that you described above, but with fewer steps. uniqid() is based on microseconds as well but returns an alphnumeric value. $filename = uniqid("Report");$filename .=".txt";would return something like this: Report1d2rt35d78h14.txt Happy coding, vujsa
  4. I've used CuteFTP for about 10 years now. Probably not going to change anytime soon. But I started using it when there weren't a lot of choices. Command-line ftp is so labor intensive, always needed to keep a cheat sheet nearby. Currenlty using CuteFTP 4.2, the upgrades are inexpensive but the version I have now suits my needs. Came with a trial of CuteHTML a text editor that has some nice features. Of course I like all script editors the have line numbers!Happy Transfering, vujsa
  5. Wow, Compuserve!I completely forgot about Compuserve. Maybe if their service was as good as their Graphics Format they would still be around.Actually, I didn't even think anyone here was old enough to remember Compuserve!lol vujsa
  6. Good Comments Make Good HTML. Commenting makes HTML easier to write. This is a spin off from another article I wrote entitle Good Comments Make Good Scripts. While the code is different, the concepts for comment are the same. Enjoy! Disclaimer! This tutorial is intended to be used to show the benefites of commenting your HTML in order to write clean, easy to read code. For the purpose of this tutorial, HTML, XML, XHTML validating is not taken into consideration. While I will make an effort to point out non-standardized code, validation has never been my priority and I'll probably miss a few things. Always run your HTML through an HTML validater to check for compatability issues. Reasons for commenting HTML: - Makes your code easier to trouble shoot. - Add a friendly reminder of things you wanted to include in your page. - Allows you to save information about the page without showing the general public. - Provides a way of giving yourself credit for creating a nice page. - Used as instructions for JavaScript and other client side script usage. Getting Started! ------------------------------------------------------------------- An HTML comment tag is quite simple, everything inside the tag is hidden from the browser and is considered to be the comment. The HTML comment tag is: <!-- --> Example with a comment: <!-- Here is a comment! --> Example of a multi-line comment: <!-- I would like take this oppurtunity to descript the color of the sky.The color of the sky is a unique shade of the color blue.Unless of course there is overcast and then its more like gray.Then again in the fog evreything is white.Appearently the sky is multicolored! --> Now lets talk about when and where to comment yout HTML. The first place I like to add a comment in a web page is at the beginning of a table. Example: <!-- Start table 1 here - 3 rows high and 4 columns wide --><TABLE BORDER="1"><TR>... Another really good use for a comment in a table is when a cell spans multiple rows or columns. Say that you have a table that is 4 rows and 4 columns and you want one really large cell in the middle. Attachment: Comments will help you keep track of where cells used to be. Example: <!-- Table 1 - 4 rows high and 4 columns wide --><TABLE BORDER="1"> <TR> <TD> Cell 1-1 </TD> <TD> Cell 1-2 </TD> <TD> Cell 1-3 </TD> <TD> Cell 1-4 </TD> </TR> <TR> <TD> Cell 2-1 </TD> <TD COLSPAN="2" ROWSPAN="2"> Cell 2-2 </TD> <!-- Cell 2-3 would have been here --> <TD> Cell 2-3 </TD> </TR> <TR> <TD> Cell 3-1 </TD> <!-- Cell 3-2 would have been here --> <!-- Cell 3-3 would have been here --> <TD> Cell 3-4 </TD> </TR> <TR> <TD> Cell 4-1 </TD> <TD> Cell 4-2 </TD> <TD> Cell 4-3 </TD> <TD> Cell 4-4 </TD> </TR></TABLE><!-- End table 1 here -->I use this frequently as I tend to get confused when I start spanning cells. Occasionally I have very lengthy forms that I need to comment in order to keep track of what's what. Example: ........<INPUT TYPE="TEXT" NAME="Var126" SIZE="25"> <!-- This is the 126th input variable for this form -->........ Comments are great for saving places for all of our content.For example, say you have an advertiser with a banner and just below his banner you have room for one more banner, just comment that this is where you can place a new add. Example: <!-- Start Foo.com's Banner Code Here--><A HREF="http://forums.xisto.com/no_longer_exists/ SRC=="http://forums.xisto.com/no_longer_exists/404.png;-- End Foo.com's Banner Code Here --><!-- To Quit Losing Money Place Advertisement Here!;) --><H1><CENTER>My Title Here</CENTER></H1> This works great for code you don't know how to write yet as well.Example: Fill in the form below to order your free money:<BR><!-- Use some of my money surplus to learn how to add an HTML form here! -->If you have any questions <A HREF="mailto:jdoe@foo.com">Email Me!</A><BR> Okay here's an example of what didn't work: <FONT COLOR="RED"> <!-- Yellow font does not work here you already tried it three times! --> Another method I use to help keep track of the flow of HTML is indenting. Example: <HTML> <HEAD> <TITLE> My Title. </TITLE> </HEAD> <BODY> Body content here. </BODY></HTML> See how easy it is to check if all of your closing tags are present.This is great if you have tables inside of tables, but will always make reading the code a lot easier. You may have noticed that all of my tags use uppercase type. I have found that this makes picking the tags out from all of the content text much easier. When I learned HTML, tags were always like this but now validation requires lowercase type as I understand it. You will have to decide for yourself how best to write your code but I suggest that you keep in mind the possibility that someday you may need to convert your HTML to another markup language. Tells us about your suggestions for commenting. Happy coding, vujsa
  7. MC, Thanks for the reply. Here's a small typo you may want to edit: $first_lightbulb -> SwitchPosition('On); // switched on our first lightbulb # # # *** MISSING END QUOTE.print_r($first_lightbulb);echo "<br /><br />"; I have read your posts over ang over and I am beginning to understand how classes behave but their use is still unclear. That's my vain way of saying that what you are saying makes sense but not to me. I tend to identify a problem and then find the quickest most direct route to its solution before identifying another problem. So if I really concentrate, I can follow along with the class code but don't really understand what's going on. I understand that the class collects variable information and uses functions to group the data into a predefined format but then what. It's hard to explain what I don't understand because I don't know what I'm confussed about. Thanks, vujsa
  8. Hey, thanks for the feedback. I agree that the title portion is a little lacking but I didn't want to get too side tracked. I considered leaving the title out alltogether as it is not a requirement to make the browser parse the code. But then I wouldn't have had a reason to have a head. While a missing head will go unnoticed by a browser, its important to learn how and where to write the head code. The title tag will be explained further in the second article. In fact the second article will discuss each of the tags from the first article extensively.Thanks,vujsa
  9. HTML 101 - Web Design For Beginners Absolute Basic HTML Writing This will be part one of a multi-part HTML tutorial. Please don't post advanced HTML replies to this article. This tutorial is specifically written for beginning HTML writers. Requirements: Software: Web Browser, HTML Editor. Skills: Ability to press the keys on the keyboard. Any web browser will work as long as it can open files saved on your hard drive. -> Netscape and Internet Explorer are both free and easy to use. Any HTML editor will work as long as it is an HTML editor and not a web page design program. -> The difference between the two is who actually writes the code. --> If the user writes the HTML code the software used is and HTML editor. --> If the user arranges the look of a page and the program writes the corresponding HTML code, the software used is a web page design program. You're here to learn HTML so you'll need an HTML editor. An HTML editor can be any text editor that will allow you to save your files with the .html extension. For now, Notepad will do just fine as your editor. I assume that Mac still has a Notepad program. <---------------------------------------------------------------------------------------------------> Now a little background about HTML. Hyper Text Markup Language is the language developed to display information on the internet in a format other than plain black text on a white background. HTML requires the use of tags. A tag is a trigger that tells the browser what to do with the information between the opening tag and the closing tag. For Example: <CENTER>Center this text!</CENTER>Here the "center" tag is used to place text evenly between the left and right sides of the window like below: This Text is Centered! With very very few exceptions, ALL open tags must be closed! Again, ALL open tags must be closed! For the "center" tag, the opening tag is <CENTER> and the closing tag is </CENTER>. the slash must be included in all closing tags and ommited in all opening tags. A Few More Samples: Align to the right is: <RIGHT> Text </RIGHT>Align to the left is: <LEFT> Text </LEFT>Bold text is: <B> Text </B>The HTML tag is: <HTML> Code </HTML>Just remember, ALL open tags must be closed! <---------------------------------------------------------------------------------------------------> The first tag that you have to use in all webpages is the HTML tag. Failing to insert this tag before your code will result in everything being shown to the user. This is because if you don't tell the browser the the page is html, the browser automatically assumes that the page is just text."<HTML>" Tells the browser that everything that comes next is coded in HTML until the </HTML> tag is reached. Step 1: Open and close HTML. <HTML></HTML>The next thing you'll need is a head. The HTML head holds very important information about your web page but for now it will only hold its place.The head tag is <HEAD> and tells the browser that everything that follows belongs in the head area of the web page until the </HEAD> tag is reached. Step 2: Open and close head. <HTML> <HEAD> </HEAD></HTML>Inside your head tag you can place the title tag. Step 3: Add a title <HTML> <HEAD> <TITLE> Here is my first web page! </TITLE> </HEAD></HTML>After you head tag comes the body tag. The body tag contains all of the information that is displayed in the browser window. Step 4: Add a body <HTML> <HEAD> <TITLE> Here is my first web page! </TITLE> </HEAD> <BODY> </BODY></HTML>Now add your content between <BODY> and</BODY> Step 5: Add your content <HTML> <HEAD> <TITLE> Here is my first web page! </TITLE> </HEAD> <BODY> Place your content here! </BODY></HTML>Cut and paste this code into your HTML editor and save as firsthtml.html and open with your browser. You'll see that you have created your first web page although it's not very pretty and it just says "Place your content here!". Help for saving and viewing your HTML documents is located at the end of this article. This is the absolute minimum code needed to create a properly working web page. Next play around with you formating a little like so: <HTML> <HEAD> <TITLE> Here is my first web page! </TITLE> </HEAD> <BODY> <CENTER>My First Page.<BR> </CENTER>More text here.<BR> </BODY></HTML>The <BR> tag means line break and is one of a very few tags that does not reqiure a closing tag. Next we'll add a link to another page or site. A link is actually called an anchor and thus the tag is <A> to open and </A> to close. The tag actually will require some formating of its own as seen below. <A HREF="http://forums.xisto.com/ Text</A> "A" is the tag type. "HREF" is an argument that tells the browser that this anchor is Hypertext REFerencing whatever is after the equal sign in quotes. The "Sample Text" is the text that can be clicked on to take you to "http://forums.xisto.com/;. ***See note at bottom about the definition of "HREF". Here it is in your you code: <HTML> <HEAD> <TITLE> Here is my first web page! </TITLE> </HEAD> <BODY> <CENTER>My First Page.<BR> </CENTER>More text here.<BR><A HREF="http://forums.xisto.com/ Text</A><BR><A HREF="https://www.paypal.com/de/webapps/mpp/home here yo go to PayPal!</A><BR> </BODY></HTML>Okay, that should do for now. This tutorial is just a basic reference for beginners and will be referenced in future tutorials. I just wanted to break the ice and provide some HTML comfort for more advanced topics. File Saving And Viewing Instructions Here! To save an HTML file in Notepad: Click File and then Save As Type your name and make sure that the .txt is gone and that the file name ends with .html --> like:myfile.html Then where it askes for file tye change from Text Documents (*.txt) to All Files Click Save. Remember which folder you saved the file in, probably in My Documents. To view an HTML page with Netscape: Click File and then Open File. Browse to the location of your html file and choose open. To view an HTML page with Intenet Explorer: Click File and then Open and then Browse. Browse to the location of your html file and choose open. *** HREF Note: I corrected this definition after microscopic^earthling reminded me of what the "H" stood for in HREF. My thanks to microscopic^earthling. Happy Coding vujsa
  10. harriko, If you are trying to learn PHP and already know HTML, I would suggest writing a PHP script that simply makes a normal static HTML page in order to get used to the program basics and simple string functions. Small Example: <?// First let's write the HTML - HEAD$html = "<HTML>\n";$html .= "\t<HEAD>\n";$html .= "\t\t<TITLE>\n";$html .= "\t\t\tMy First PHP Project\n";$html .= "\t\t</TITLE>\n";$html .= "\t</HEAD>\n\n";// Next let's write the HTML - BODY// and fill it with content!$html .= "\t<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#0000FF\" VLINK=\"#800080\">\n";$html .= "\t\t<H1><CENTER>My First PHP Project</CENTER></H1><BR>\n";$html .= "\t</ BODY>\n";$html .= "</HTML>\n";// Now let's output that HTMLecho $html;?> This will give you a platform to experiment with. Happy Coding, vujsa
  11. Loaded in about 4 seconds here but my broadband is rated at 4mb/sI don't think that you'll be able to get rid of the banana. I might suggest contacting the author and inquiring about purchasing a clean copy (without the credits visible).Happy coding vujsa
  12. It seems to me that the author may not be a native English speaker. Minor inconsistancies in the grammer throws off the pace of the reader. Most people wouldn't even be conscience of the flaws but an unconscience awareness could be a real turn off for some people.I could be mistaken, or perhapes a little grammer check may be needed.I agree that the page seems to be too cluttered.I suggest that you actively advertise the free hosting on the paid hosting site and vise versa. Perhapes the interest will cause some one to try your free service prior to buy your other service.There needs to be a bigger difference between plans. Just storage and BW differences will make large clients feel that they are underapprecieted. People need to be made to feel special.I know that certain services are free but should be by request only.Perl, PHP, MySQL are common freebies but use server resources. Hold some things back just for the big clients. If the little guy wants it, just give it to him. By the way, if the customer will only pay $9.95/yr for a host, probably can't afford microsoft frontpage. I don't know for sure as I don't use FrontPage.Why offer unlimited Parked Domains, MYSQL Databases, Sub-Domains. Throw them in on Business packages but charge a minimal fee for more than one otherwise. You seem to be offering BMW's to everyone at varying prices. good luckvujsa
  13. MC I tried to complete the assignment you gave me. How'd I do? <?/*Assignment from mastercomputers -->To really understand classes you really need an idea of what you're trying to achieve,say for example you have a light bulb, the light bulb is the object (class), what can a light bulb do?Well switch it on, the bulb will shine brightly, switch it off, and it won't.We could create an object light bulb and can also create variables to store it's status and a functioncalled switch to change it's status. Do you think you could write such a class?*/class Lightbulb { var $name; var $status; function Switched($name, $status) { $this -> name = $name; $this -> status = $status; } // End function Switched()} // End class Lightbulb$new_lightbulb = new Lightbulb;$new_lightbulb -> Switched("Red", "On");print_r($new_lightbulb);?>Returned:lightbulb Object ( [name] => Red [status] => On ) I assume that when I finally understand the concept it'll hit me like a ton of bricks. Thanks, vujsa
  14. microscopic^earthling, Excelent tip. That may be the most important idea I've ever recived in a forum. I can't begin to count the number of times I've left a closing bracket out and couldn't find it. Playing the open open close open open close open close close open close close game. Sometimes you don't even get error or warning messages just nothing works. Which leads me to a good follow up topic. Indenting your code will help to clean your scripts up. Such indenting will provide as much information about your scripts as comments do. Indentions help to identify command relationships and makes debugging you code easier. Dependind on where you learn PHP an indent is either 4 or 5 spaces or a tab. Tabs don't always show correctly in some script editors. Standardization seems to be leaning towards 4 spaces and never tabs. Here is an example of proper indentation: <?$a = 1;$b = 2;$c = 3;if ($a == 1) { function apple($x,$y,$z) { $answer = $x +$y + $z; return $answer; } // End function apple()} // End IF Statementecho apple($a,$b,$c); // Output Data?> Happy coding vujsa
  15. So this is a basic programing tool that is too often overlooked. I learn programing by looking at other peoples work and figuring out what each command does. Having comments in the code provides me with an idea of what the author was thinking while writing that part of the code. Additionally, commenting your code allows the code to be modified by others for uses other than what it was originally written for. This type of versitility makes the overall value of the script higher. Many programmers believe that they should protect their scripts by intentionally making them difficult to read. This actually makes the scripts less desireable because fewer people can modify or fix such scripts. So I'd like to take this opportunity to explain how to comment your code, when to comment your code, and what comments you should make. PHP comments are made with a double slash // preceeding the comment. <?// PHP comment goes here.?> The double slash is only good for one line of code so new double slashes are needed for each line of comment. <?// If one line of commenting is no enough or undesirable,// a second line can be added this way.?> The double slash has yet another use. a comment can exist on the same line as the code which it describes as long as the comment comes after the PHP code. <?// Let's add some valid PHP code below such as a variable assignment.$foo = "foo_value"; // the value of $foo is "foo_value".?> While the double slash is the commenting method devoloped for PHP, the double slash is not the only commenting tag that PHP will allow. Perl users will be happy to know that shell comment tags are welcome as are C program comment tags. The pound sign # is used for comments in shell programs like Perl and are used the same way as the PHP double slash. The comment follows the pound sign, a pound sign is only good for one line of comment, and a comment can share the same line as code as long as the comment comes after the code. <?# Shell-style comment tags like those used in Perl look like this.# And the comment tag is only good for one line.$foo = "foo_value"; # Shell comments can also come after the code like this.?> C-style comment tags are a little different. The slash star - star slash /* */ tags require the comment to be opened with a slash star and closed with a star slash. <?/* Place your comment here and then close the comment */?> The slash star - star slash comment tags require a bit more work but the work pays off. The comment can span accross several lines as long as the comment is correctly opened and closed. <?/* My comment can be as long as I want,it can even use several lines.Whenever I get done commenting, I simply close the the comment. */?> Additionally, the C-style comment can come before or after any code as long as the open and close tags are used properly. <?/* Let's describe the code below,the code below is a variable definition. */$foo = "foo_value"; /* The value of $foo is "foo_value" *//* But we have a second variable */ $widget = "5"; /* The value of $widget is "5" */?> ############################################################ #### Code Commenting Part Two - When To Comment #### ## ########################################################## Sorry, I know that that is rude. Above please notice a block comment using a shell comment tag. When a new section of code is started, you may want to use a bold statement like a block comment to introduce the next section. This may occur after defining all of your functions and before coding your output such as HTML. The most common use of the block comment is at the top of the script to identify the script name, use, version, copyright info, and author info. Example: ############################################################# #### Code Commenting Tutorial Script In PHP. #### To be used by new programers to explain commenting #### Version 1.1 #### Non Copyrighted Material #### Witten By John Doe #### ## ########################################################### Comments before functions, especially complex functions, describing what the function does even if the function is pre-defined in PHP. This way if a bug pops up in the program, you can quickly identify the problem function. <?// build_input_field() uses the arguments collected by the function call to create an HTML form input field.function build_input_field($type,$name,$value,$size,$maxlength) {$field = "<INPUT TYPE=\"$type\" NAME=\"$name\" VALUE=\"$value\" SIZE=\"$size\" MAXLENGTH=\"$maxlength\"><BR>\n";return $field;}?> Another good place for a comment is near code that you have had or are having trouble writing.Say you know what you want the code to do but are confussed about how to write it, Add a comment in the place in your code where you are having problems. The comment should describe what you wantto accomplish and what is causing problems. This will help keep you focused on your goal. Many times the answer is obvious once we stop looking what went wrong and start looking at what went right. Too many people simply delete the problem code and start from scratch because of frustration. The frustration comes from loosing focus of the logic you wanted to use. Comments remind you what the plan was. Comments also make it easier for others to help you fix problems. Example: <?// Next I'd like to write a function that will automatically build a HTML form based on information// found in an SQL database table.// First I need a master function that will controll smaller functionsfunction foo_master() { function foo_secondary_1() { if (condition) { $x = 1; return $x; } function foo_secondary_2() { if (condition) { $x = 2; return $x; } }}?> Obviously, this code doesn't actually do anything. Commenting can make a good script look great. Not only will comments help others to use your code but will make your code easier to write, fix, and modify. Try to keep your comments on topic, jokes and personal comments just clutter the code. Sometimes the best comment is to let a piece os code speak for itself. Remember, describe the code and what you want it to do and you'll find that your scripts will be easier to write. Happy coding vujsa
  16. So the code provided returned this: invoice Object ( [name] => John Doe [address] => 123 Someplace St [date] => 21/02/2005 [phone] => 555 5555 ) At least your example will output a result. So print_r() shows me that the variable $new_invoice is actually an object named invoice. And the object named invoice contains information about John Doe. Am I reading this correctly? If so, could you show an example of how to use the data contained in the object. Also, if I wanted to use more than the one entry, I'd need to save the John Doe information first or use a variable key [$x] and an incremental command with $new_invoice Basically, save $new_invoice in a database or flat file or change $new_invoice to $new_invoice[$x] and create an array. Sorry, sometimes I need to re-explain things to myself to work it all out in my head and then to others to get feedback. Thanks vujsa
  17. I've learned every programing language I know by example. BASIC, JavaScript, PHP, Perl, HTML. HTML I actually learned by printing a copy of the source code of the Indiana University index page and printing the page as well. Then I went line by line figuring out each command. The only language I every actually had former instruction in is C++. You'll notice it's not in the list of programming languages I know. So maybe I'm not a master programmer in any of these languages but I can accomplish most of my requirements. The downside is that many times I write much more code than I need to. I just don't know the better way. I'm sure that using Classes in my scripts would make my life far easier and the scripts simpler if I knew how they worked. The PHP Manual is less than helpful on this topic at least for me. I've tried other PHP websites but their information is just a duplicate of the PHP Manual. So, if someone could please explain the concept and provide a useful example it would be very helpful. Oh, by the way, feel free to over simplfy the expaination. Thanks, vujsa
  18. On a related topic, .= has a derivitive. . (dot) can be used in complex variable assignments. For example if you wanted to use an inline function use the following: $html = "PI = ".number_format(M_PI,"2",".","")." = ".M_PI."!"; Where the RED code is outside of the quotes so that the function will execute correctly. Otherwise the output would read: PI = number_format(M_PI,"2",".","") = M_PI! Instead of: PI = 3.14 = 3.14159265359! Other wise written: $html = "PI = ";$html .= number_format(M_PI,"2",".","");$html .= " = ";$html .= M_PI;$html .= "!"; Just to recap. The . (dot) means plus or and. This allows strings to be mixed with functions etc... Saves from repetitive coding and as a result time. Makes the code cleaner and easier to read when an entire string "sentence" is all together on the same line. Speaking of eliminating repetitive coding, see the following post of mine:Rapid HTML code generation using simple PHP Many other good PHP links in the same forum. Good luck vujsa
  19. Actually, Called SSI but not used for PHP PHP code is <? include(filename.php); ?> must be called from a PHP enabled page like: index.php index.phtm index.phtml testpage.php ... additionally, .php, .phtm, .phtml must be listed in apache to forward to the php engine. so if you only listed .php, then the calling page must have that file extension. unless, you use an html script tag like: <script language="php">include(filename.php);</script> the included file can have any extension like: .inc, .php, .txt, .code good luck, see also Programing How to's / Programming / PHP-> PHP forum vujsa
  20. I don't know about the rest of you, but I love writting scripts but hate WRITTING scripts. For example, how many times do you think you have typed the following. Example #1: <INPUT TYPE="TEXT" NAME="Foo" VALUE="Foo Value" SIZE="25" MAXLENGTH="100"> Select fields are worse, especially if you write clean code like I do with indents and seperate lines for each tag.Example #2: <SELECT NAME="Fruits"> <OPTION>Apples</SELECT> <OPTION SELECTED>Oranges</SELECT> <OPTION>Grapes</SELECT> <OPTION>Peaches</SELECT></SELECT> Having been writting HTML for 10 years now, so I look for as many shortcuts as possible. Now I let PHP write all of the repetitive HTML and I just fill in the blanks.From Example #1, all we needed was the information in the quotes: "TEXT", "Foo","Foo Value","25","100" Everything else is just adding to your carpal tunnel syndrom. so lets get rid of it! You'll need a function: Function #1: <?function build_input_field($type,$name,$value,$size,$maxlength) { $field = "<INPUT TYPE=\"$type\" NAME=\"$name\" VALUE=\"$value\" SIZE=\"$size\" MAXLENGTH=\"$maxlength\"><BR>\n"; return $field;}?> Now call your function.Call #1: <? build_input_field("TEXT","Foo","Foo Value","25","100"); ?> The above would automatically create the code for example #1. Now adding your own formating to the function will allow you to output code exactly the way do needed it. Building a select field requires a little more effort. I had originally considered simply explaining the the differences between writting the code for an input field and a select field and referencing the previous Function and Call. But after some thought, I reallized that for most beginners the loops, call, arrays, and conditions needed in unison may prove to be too confusing. So I'll expain how the entire process works for the script I wrote to build select fields from a one line call embedded in standard HTML. Let's revisit example #2 Example #2: <SELECT NAME="Fruits"> <OPTION>Apples</SELECT> <OPTION SELECTED>Oranges</SELECT> <OPTION>Grapes</SELECT> <OPTION>Peaches</SELECT></SELECT> In Example #2 the required information is:Field Name -> "Fruits" Options -> "Apples, Oranges, Grapes, Peaches" Selected Option -> "Oranges" We have three main arguments for our function a four secondary arguments. for the Options argument. Looking at the Options argument, it looks very similar to an array. $options = array("Apples","Oranges","Grapes","Peaches"); So our call should look something like this: Specific: <? build_select_field("Fruits",array("Apples","Oranges","Grapes","Peaches"),"Oranges"); ?> General: <? function_name("Field_Name",array("Option1","Option2","Option3","Option4"),"Option Selected"); ?> Now that we have a Call, we can write a function that can convert the Call into usable HTML. Writing the function. Step 1 - Name your function and list arguments. function build_select_field($name,$select_array,$selected) { Step 2 - Start writing select field code. Simply define a variable to hold the first line of the select field code. $select = "<SELECT NAME=\"$name\">\n"; Step 3 - Insert each option. Add a FOR loop to get each value from the array in this case four different fruits. Use the count() function to find out the total number of values in the array then execute the code below for each value. for ($x = 0; $x < count($select_array); $x++) { Step 4 - Get the selected value. Use an IF statement to determine if the current value from the array is the value that should be selected by default. if ($select_array[$x] == $selected) { Step 5 - Write the <OPTION SELECTED> code. Just add the new HTML to the previous information contained in $select using " .= " instead of " = " only. Don't forget to close the IF statement with " } " $select .= "<OPTION SELECTED>$select_array[$x]</OPTION>\n"; } Step 6 - Write the <OPTION> code. Use an ELSE statement to write the non-selected option code again adding the new HTML to the old with " .= " and not " = ". Don't forget to close the ELSE statement with " } " else { $select .= "<OPTION>$select_array[$x]</OPTION>\n"; } Step 7 - Finish up. Close the FOR loop with " } ", close the Select tag with </SELECT>, Return a value, and close the function with " } ". } $select .= "</SELECT><BR>\n"; return $select; } The final code should look like the following: function build_select_field() function build_select_field($name,$select_array,$selected) { $select = "<SELECT NAME=\"$name\">\n"; for ($x = 0; $x < count($select_array); $x++) { if ($select_array[$x] == $selected) { $select .= "<OPTION SELECTED>$select_array[$x]</OPTION>\n"; } else { $select .= "<OPTION>$select_array[$x]</OPTION>\n"; } } $select .= "</SELECT><BR>\n"; return $select;} So this would be quite silly to use if you need only one or two select fields unless you place your function in it's own file and include that file's contents in pages that use forms. But if you have several select fields and/or many options in each field, this could save you a lot of time.With a little more PHP or a modified call, the build_select_field() function can build a select field from any array including databases like MySQL. Hope this helps cut your work load coding forms. This same technique can be applied to Tables as well but I love the idea for headers and footers. Every page in you website can have the same appearence by simply writing a code for all of the pages HTML code except that which is specific to that page. That way your format would be uniform while the content changes. Happy coding vujsa
×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.