University of Bahrain
Information Technology
College
Computer Science Department
Semester 1, Year 2006/2007
ITCS 373: Class Lab Work
PHP1
Instructor: Dr. Eshaa M.
Alkhalifa
Start by installing AppServ
2.5.6. You enter localhost
for your server name, and your email address or any email. Then type a password for the root that you
are likely to remember. Your correct
folder to store all your php files is c:/appserv/www/ You can make a folder for your work or just
add the files to the same folder.
- Our official
editor for PHP will also be HTML Kit which scored 4/5 in user reviews for
PHP scripting. If you wish to use a
specific PHP editor, I can recommend PHPedit
which scores 5/5 in user reviews.
Some of the others score worse than HTML kit so I don’t advise you
to just try anything. Learning how
to use a new editor may waste precious time.
- Open a new
script and start your PHP scripting using the tag <?php and for you not to forget write the closing
tag from now ?> and insert your script between the two.
- In order to
test that the PHP interpreter is working well, write the line echo This
is my first PHP script; between the two and then save your script in
the correct folder using the name myscript.php.
- Go to a browser
window and type localhost/myscript.php. If you did your work correctly you will
see the sentence This is my first PHP
script displayed on the screen.
- Now we will use
as a form, the same HTML form that you wrote in HTML3 lab. You will notice that your form has
different types of fields, that will be of
benefit to us in learning the different types of commands you can use in
PHP.
- We will allow
the visitor of the page to write the data into the fields and send the
form to us so that we can process the information. The script’s first job is therefore to
receive the information that is sent from the form. To do this you must check all the
‘names’ you gave to the different fields of data in your form. You must use the same names in your
script to receive the data these variables hold.
- Write this as
your first line in your PHP Script $rating = $_POST['rating'];
It will receive the data that is contained in the field with the name
rating that is sent by the form using the POST (this is written following
the word method in the HTML form.
The data will be placed in the variable $rating. All variables in PHP start with a $
sign.
- Now use the echo instruction to display
the value of the variable by typing
echo "<h1> These are the results
obtained from your form"</h1>";
echo "<br>The
rating of your page is (1-5) ";
echo $rating;
- You can repeat
the same lines for the other data you collect from the user by adding the
following lines;
$picture = $_POST['picture'];
$truth = $_POST['truth'];
$comment = $_POST['comment'];
$option1 = $_POST['option1'];
$option2 = $_POST['option2'];
$option3 = $_POST['option3'];
$option4 = $_POST['option4'];
- Then you can display the contents of the
variables using the lines;
echo "<br>1. The opinion of the visitor of the pictures in
the site is (1-5) :";
echo $picture;
echo "<br>2. The opinion of the visitor of how truthful you
are about your grades is (1-5) : ";
echo $truth;
echo "<br>3. The comments the visitor made of your page are : ";
echo $comment;
echo "<br>4. The parts the visitor liked in your page are :";
echo $option1;
echo " ";
echo $option2;
echo " ";
echo $option3;
echo " ";
echo $option4;
- Save the file
with the same name myscript.php and copy your
html form into the same folder that contains your php
script. Make sure that the name of
the form is myform.htm. Open the form with HTML Kit and alter the line
that says
<form action="http://itcs.silvertair.com/files/Clabs/myfirst.php"
method="POST">
Replacing it with the line
<form action="myscript.php"
method="POST">
- Now you are
ready to see the output. Open a
browser window and write localhost/myform.htm. Fill in the fields and take a look at
the output. The output is different
from what you got when you used the script that I have on the silvertair site in that this time, you will get
numbers for each type of answer.
- You can now
test what it means to place a GET method in the form tag so copy and paste
the following part of your code to a new form.
<form action="http://www.itcs.silvertair.com/files/Extras/GET.php"
method="GET">
<ol><li>
How would you rate my page?</li>
<br>1<input name="rating"
type="radio" value="1">
2<input name="rating" type="radio"
value="2">
3<input name="rating" type="radio"
value="3">
4<input name="rating" type="radio"
value="4">
5<input name="rating" type="radio" value="5">
<input type="submit" value="submit"
name="submit">
</ol>
</form>
- This form is identical to yours with one
difference which is that it uses the method GET. In the php
file you will also use the command $rating = $_GET['rating'];
to be able to access your data and display it. In this case, you will be using a php script that I wrote for you.
- Notice in the
URL you will see the following; http://www.itcs.silvertair.com/files/Extras/GET.php?rating=3&submit=submit
It says that the rating=3 so you can guess what rating I gave to test
my form because it just adds what I type to the URL.
- Because it does
this, GET is only used when short inputs are expected as in Google. Type a word to search for something on
Google and see what the URL looks like when your results show up. You will find the word you typed.
Now you can test how to use basic operators on
your php script.
So add the following lines
echo "<br>The
total rating is ";
echo $rating+$picture+$truth; immediately below the line echo $truth;
- Save your file at the correct location
calling it myscript.php and then go to your
browser and see your output. You
should see an additional line that looks like this;
The total rating is 10 The number 10 appeared because I chose the ratings 4,2,4 so when we add
them we get a 10. You can use the
operators for various calculations and you can store the results in new
variables.
- Arrays:
Arrays in PHP can be either numerically indexed or
associative. A numerically indexed
array looks like this $employee_array[0] =
"Bob"; while an associative array associates what in the square
brackets with the value following the equal sign like this $salaries["Bob"] = 2000;
The first says that Bob is the name stored in the first array cell,
while the second says that the cell with the name Bob has a salary of
2000.
- We will try
numerically indexed arrays for our example, by replacing the lines
$option1 = $_POST['option1'];
$option2 = $_POST['option2'];
$option3 = $_POST['option3'];
$option4 = $_POST['option4'];
with the lines
$option[1] = $_POST['option1'];
$option[2] = $_POST['option2'];
$option[3] = $_POST['option3'];
$option[4] = $_POST['option4'];
and replacing the
lines
echo
$option1;
echo
" ";
echo
$option2;
echo
" ";
echo
$option3;
echo
" ";
echo
$option4;
with the lines
echo
$option[1];
echo
" ";
echo
$option[2];
echo
" ";
echo
$option[3];
echo
" ";
echo
$option[4];
- You should know
now how to save your work and check if it works correctly.
- Looping:
Although we used arrays, we did not take full advantage of what
they offer because we did not use any loops. We can do that by going to the output
lines
echo
$option[1];
echo
" ";
echo
$option[2];
echo
" ";
echo
$option[3];
echo
" ";
echo
$option[4];
and replacing them with
for ( $counter = 1; $counter <= 4; $counter ++)
{
echo " ";
echo $option[$counter];
}
- If then else:
We can also add conditional statements to our script like adding
the line
if ( $rating >= 3 ) {
echo "WOW!! Your page rocks!! <br><br>";
} else {
echo "YUCKS!! Your page sucks!! <br><br>";
}
Add it just below the line $rating = $_POST['rating'];
- Test the result
of saving your script and trying it with different values.
- Switch: This allows you to test different
possibilities and giving a reaction to each one of the possibilities.
- Add the lines
switch ($truth){
case "1":
echo
"<b>Consider yourself not welcome to visit this page</b>";
break;
case "2":
echo "<b>Now would it really hurt you to
give me a higher truth rating?</b>";
break;
case "3":
echo "<b>Not bad at all.</b>";
break;
case "4":
echo "<b>Cool, you will have no problems with me.</b>";
break;
case "5":
echo "<b>YES!
That’s the right attitude.</b>";
break;
}
- Check the output
of your work.
- Functions: You can now add a function to your
script by adding the following lines following the opening tag for php,
function myCompanyMotto(){
echo "<h1><center>Thank You My
Guest!</center></h1><br />";
}
- You can now
call the function using the code myCompanyMotto();
- Now see your
form, fill out the information and see the output. The result depends on the choices you
make but it may look like this;
