While Loop

What to do

Create a "while" loop that will produce a comma-separated listing of numbers from 10-30. Be sure that the condition makes the loop stop printing to screen at 30 and not 31.

Here's the Result

10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,

For Loop

What to do

Do the same as you did for the "while" loop, except use only "for" loop syntax to achieve the same results.

Here's the Result

10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,

Foreach Loop - Indexed

What to do

Create an indexed array with 5 of your favorite beverages. Then echo this array back in an unordered list format using PHP below.

Here's the Result

  • water
  • cherry pepsi
  • code red
  • apple juice
  • fruit punch

Foreach - Associative

What to do

  1. Create a new associatve array called $nav_links whose keys and values are:
    • 'Yahoo'=>'http://yahoo.com'
    • 'Google'=>'http://google.com'
    • 'Bing'=>'http://bing.com'
  2. Next, create a foreach loop that accesses the $nav_links array and echoes each item back as functional links inside of an unordered list item below.

 

Here's the Result

Basic Select/Option List Forms

What to do

Let's create a simple Select/Option form with the following values:

URL Name
#select Select a Search Engine
http://yahoo.com Yahoo
http://google.com Google
http://bing.com Bing

You can either make the Option/Select form by hand or with Dreamweaver.

Here's the normal html form:

Select/Option Form Generated From PHP Associative Array

What to do

First, let's make a new associative array called $nav_links_form with these keys/values:

'Select a Search Engine' => '#select' 'Yahoo' => 'http://yahoo.com' 'Google' => 'http://google.com' "Bing' => 'http://bing.com'

Next, let's create a basic Select/Option form in html (outside of php tags) with only one "option" tag and a submit button. Don't forget to correctly associate the label tags to the "select" and and button "input" tags. Then embed the opening line of a php foreach loop accessing the $nav_links array just before the "option" tag. Close this tag after the opening curly brace and end the php tag. Next, create a php open/close tag set in place of the url address value in the "option" tag, and echo the temporary value variable in this php block. Next, create a php open/close tag set in place of the html link value in the "option" tag, and echo the temporary key variable in this php block. Place your cursor at the end of the option tag, create a new php block, and echo a "new line" character, and then type in the closing curly brace. Essentially, what you are doing is enclosing the "option" tag html inside of the loop commands so that it will create a new option tag populated with the correct array values to create the drop down list options.

What to do (Step 2)

  1. Go into the form and make sure that the "name" attribute is set for the "select" and submit button "input" tags. If you follow the demo, the name tags will be set as follows:
    • select : "engines"
    • input button : "go"
  2. Next, go to the top of the "four.php" page in the root site folder. Remove ALL white space from thebeginning of the file before the DOCTYPE and make a new PHP block at the top.
  3. Per the demo, create a new if/else statement that 1) tests to see if the post of "go" is present, and if so, it redirects us to the correct search engine page, and 2) if "go" is not present, write a command redirecting us back to this original parent page (four.php). Make SURE the file pathways are correct!
  4. Be certain to link to $_SERVER['PHP_SELF'] in the original form's "action" attribute so that the page is refreshed to test the condition at the top of page "four.php".

Here's the Result