Friday, November 28, 2008

Lesson 7 : More Loops

Objective
This lesson will take us further into the world of If/Else Statements and While
Loops. We will use Forms to collect information from our users, and then use
If/Else Statements and While Loops to process that data.

Step 2
Type in the lines just as they appear in text given below:

form action="”lesson7output.php”" method="”POST”">Choose an Animal:
select name="”animal”"> option>Bird /select
input type="”submit”">
/form

Note: This code is strictly HTML. Do not include the line at the end of this file. Type only what appears in the box above. Do
not copy-and-paste the code.


Step 3
Name this file lesson7input.php and save it in your Project Folder.

Step 4
Open a new window in your Text Editing Program. This will be used to create the
second file for this lesson.

Step 5
In the second file, type in the lines just as they appear in text given below:



Step 6
Name this file lesson7output.php and save it in your Project Folder.

Step 7
Upload lesson7input.php and lesson7output.php to your server.

Step 8
View the first file (www.YourWebDomain.com/lesson7input.php) in your Web
Browser. You should see a text field that asks for your age.

Step 9
Choose an animal from the dropdown menu and press the Submit Button.

Step 10
You should see a new page that displays a message. Depending on the animal
that you chose, the If/Else Statements will determine what message to display.


We just created two files. The first file is a form that allows you to choose an
animal. After you click the Submit Button, you are directed to the second file. The
second file uses If/Else Statements to determine what messages to display on
the screen.
First File (lesson7input.php)
Line 1




This line of code is the beginning of the form.
The portion that says action=”lesson7output.php” tells the form to forward the
browser to the file that is named “lesson7output.php” after the user clicks the
Submit Button.

First File (lesson7input.php)
Line 2


Choose an Animal:

This line of code is a simple text output to the screen.

First File (lesson7input.php)
Line 3


select name="”animal”" This line of code begins the dropdown box. The portion that says name=”animal” tells the form to create a variable named $animal. When you click the Submit Button, the form passes the variable to the next page. First File (lesson7input.php) Line 4 – Line 7 These lines of code each create an option in the dropdown box. When you choose one of these options from the dropdown box, the Form assigns that value to the $animal variable. First File (lesson7input.php) Line 8 /select

This line of code ends the dropdown box.

First File (lesson7input.php)
Line 9


input type="”submit”"

This line of code creates the Submit Button. When this button is clicked, it will tell
the form to process the data that has been entered.

First File (lesson7input.php)
Line 10




This line of code is the end of the form.

Note: We have examined all the code from lesson7input.php . We will now
examine the code from lesson7output.php .

Second File (lesson7output.php)
Line 2


$animal = $_POST[“animal”];


This line of code is used to define the variable $animal. This code uses the
$_POST function to get the value for the variable $animal, which was sent from
the from on the previous page.

Second File (lesson7output.php)
Line 3


if ($animal == ‘Dog’) {

This line of code uses an If Statement to test the variable $animal . The value of
$animal is decided by the value that you chose from the dropdown menu.
If the value of $animal is equal to ‘Dog’, then this If Statement is executed.
If the value of $animal is not equal to ‘Dog’, then the script continues to the next
test.

Second File (lesson7output.php)
Line 4


echo “Dog says WOOF”;

This line of code uses an echo statement to print “Dog says WOOF” to the
screen.

Note: The If/Else statements each test for one of the animals. If the test is found
to be true, that If/Else Statement is executed. Once an If/Else Statement is
executed, no more If/Else Tests are run.


Exercise 7-1
You can use a form to send more than one variable at a time. Here we will create
two files that will be used to send and receive multiple variables. The second file
will be use If/Else Statements and While Loops to create an output.

Name this file “lesson71input.php”


form action="”lesson7output.php”" method="”POST”">
Choose an Animal:
select name="”animal”"> option>Bird /select



# of Noises:
input type="”text”" size="”2”" name="”noises”">



input type="”submit”">
/form>


Name this file “lesson71output.php”

’;
$speak = ‘WOOF!’;
}
else if ($animal == ‘Cat’) {
echo ‘Cat says MEOW
’;
$speak = ‘MEOW!’;
}
else if ($animal == ‘Bird’) {
echo ‘Bird says TWEET
’;
$speak = ‘TWEET!’;
}
else if ($animal == ‘Mouse’) {
echo ‘Mouse says SQUEEK
’;
$speak = ‘SQUEEK!’;
}
else {
echo ‘There was an ERROR
’;
}
while ($noises > 0) {
echo $speak, ‘
’;
$noises = $noises – 1;
}
?>



Conclusion

In Lesson 7 we used a form to collect information from the user, and then used
If/Else Statements and While Loops to produce an output.

In reality, the examples that have been presented to you in this eBook are not
practical or useful in the real world, but they will provide you with the groundwork
to build your PHP skills. You will soon find yourself creating dynamic websites
that interface with visitors and databases.

I hope you have found this Blog to be a helpful resource. If you enjoyed this and would like to see me produce more about advanced PHP Programming, please feel free to contact me or leave your opinion msgs here.

1 comment:

Rendell said...

I'm starting to learn php right, it seem this blog could help.
keep updated.
try click my ads..tanx