The While Loop

I l @ ve RuBoard

As I suggested earlier in this chapter, loops are used to execute a section of code repeatedly. You may want to create a pull-down menu consisting of the days of the month (print the numbers 1 through 31). You might want to print out each value of an array. For either of these cases, and for many more, you'll want to use a loop (I'll demonstrate the latter example in the next chapter).

The first of the two types of loops that exist in PHPthe while loopis designed to continue working as long as the condition you establish is TRUE. It will check the value of the condition before each iteration. Once the condition becomes FALSE, the while loop is exited.

 while (condition) {     statement(s); } 

To demonstrate the while loop, you'll create a script that dynamically generates a date pull-down menu (month, day, year) for an HTML form. While the form itself won't do anything as is, you'll be able to see how you can use PHP to improve upon and expedite the creation of a standard HTML form element.

To use while:

  1. Create a new PHP document in your text editor. Type (Script 6.9):

     <HTML><HEAD><TITLE>Select Menu  </TITLE><BODY><?php 
    Script 6.9. The two while loops will quickl print out all the requisite HTML to generate two of the pull-down menus (see Figure 6.16). By using the date() function to base the year loop on the current year, this script will never be outdated .

    graphics/06sc09.jpg

    graphics/06sc09a.jpg

  2. Establish the current year using the date() function.

     $Year = date ("Y"); 

    By feeding the date() function the value "Y", it will return the current year. You'll use the value to print out this year, plus the next ten, in the select menu. This way, the form will not need to be changed from year to year.

  3. Create an HTML form that includes a pull-down menu for every month.

     print ("<FORM ACTION=\"$PHP_SELF\"  METHOD=POST>\n"); print ("Select a month:<BR>\n"); print ("<SELECT NAME=Month> <OPTION>Choose One</OPTION>\n"); print ("<OPTION VALUE=January>  January</OPTION>\n"); print ("<OPTION VALUE=February>  February</OPTION>\n"); print ("<OPTION VALUE=March>March  </OPTION>\n"); print ("<OPTION VALUE=April>April  </OPTION>\n"); print ("<OPTION VALUE=May>May  </OPTION>\n"); print ("<OPTION VALUE=June>June  </OPTION>\n"); print ("<OPTION VALUE=July>July  </OPTION>\n"); print ("<OPTION VALUE=August>August  </OPTION>\n"); print ("<OPTION VALUE=September>  September</OPTION>\n"); print ("<OPTION VALUE=October>  October</OPTION>\n"); print ("<OPTION VALUE=November>  November</OPTION>\n"); print ("<OPTION VALUE=December>  December</OPTION>\n"); print ("</SELECT>\n"); 
  4. Use a while loop to create the pull-down menu for the days.

     print ("<P>Select a day:<BR>\n"); print ("<SELECT NAME=Day><OPTION>  Choose One</OPTION>\n"); $Day = 1; while ($Day <= 31) {    print ("<OPTION VALUE=$Day>$Day  </OPTION>\n");    $Day++; } print ("</SELECT>\n"); 

    The first step taken is to set the value of $Day to 1. This must be done before the loop is called. The loop itself will then see if $Day is less than or equal to 31. If it is, it will print the value of $Day as an option in the select menu and then increase the value of $Day by 1. This process will continue until $Day is equal to 32, at which point PHP will stop the loop and continue through the script. Using a loop to generate the requisite HTML is much easier than printing out 31 lines of code one at a time.

  5. Use another while loop to create the year pull-down menu.

     print ("<P>Select a year:<BR>\n"); print ("<SELECT NAME=Year><OPTION>  Choose One</OPTION>\n"); $EndYear = $Year + 10; while ($Year <= $EndYear) {    print ("<OPTION VALUE=$Year>  $Year</OPTION>\n");    $Year++; } print ("</SELECT>\n"); 
  6. The listed years in the pull-down menu will be generated based upon the current year, plus the next ten. The current year was assigned earlier to the $Year variable. The last year then, $EndYear, is equal to $Year plus 10 (this value could easily be changed to print out 5 or 15 years ). The loop states that as long as the $Year value is less than or equal to the $EndYear value, it should print out the $Year value and then increment the value by one.

  7. Create a submit button, close the form, the PHP, and the HTML.

     print ("<P><INPUT TYPE=SUBMIT NAME=  SUBMIT VALUE=\"Go!\"></FORM>\n");  ?></BODY></HTML> 

    Although this page is designed to be part of a larger HTML form and therefore doesn't do anything in its own right, it's best to be consistent so I've added the submit button regardless.

  8. Save your page as select.php, upload it to the server, and test it in your Web browser (Figures 6.15 and 6.16).

    Figure 6.15. The select.php page generates three pull-down menus allowing the user to choose a month, day, and year. The code could easily be pasted within a larger HTML form any time these menus are needed.

    graphics/06fig15.gif

    Figure 6.16. This is the source code for the form pictured in Figure 6.15. Notice how a dozen lines from Script 6.9 (containing two while loops ) generated 40-50 lines of HTML (the Day and Year select menus).

    graphics/06fig16.jpg

Tip

You also have the option of using the dowhile loop which will guarantee that the statements are executed at least once, which is not necessarily true of the while loop.

 do {    statement(s); }  while (condition); 

Tip

Be sure to think twice about what elements need to be included within a loop and which ought to be excluded. Failure to increase the value of $Day or $Year within a loop would create an endless loop (as, for example, $Day would always be less than or equal to 31). Similarly, inclusion of the <SELECT> tags within the loop will create multiple pull-down menus.


Tip

Just as the if conditional can be written on one line if you only have one statement, the same can be done with the while loop, although, again, I would recommend against it.


I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net