PHP WORKSHEET SOLUTIONS - 1

advertisement
WORSHEETS SOME SOLUTIONS
Worksheet 1 Solutions
3. Now create a php program which will:
- Assign values to 2 variables
- Add the values of the first and second variable and assign the result to a variable
called $sum (use + to add variables)
- Subtract the values of the second variable from the first and assign the result to
a variable called $minus (use +- to subtract variables)
- Multiply the values of the first and second variable and assign the result to a
variable called $product (use * to multiple variables)
- Divide the values of the first variable by the second and assign the result to
a variable called $division (use / to subtract variables)
- Get the remainder (modulus) of the division of the first variable by the second (use
the modulus operator %) and assign the result to the variable $modulus
- Print the value of each of the number variables (name plus value)
- Print the values of the 5 operations (name plus value)
- Add comments to show what is happening in the program
calculations.php
<?php
// assign values to 2 variables
$num1 = 15;
$num2 = 7;
// perform 5 operations
$sum = $num1 + $num2;
$minus = $num1 - $num2;
$product = $num1 * $num2;
$division = $num1 / $num2;
$remainder = $num1 % $num2;
// print out numbers
print "First number is $num1 and Second number is $num2<br /><br />";
// print out calculation details with results
print "Result of $num1 + $num2 is $sum<br /><br />";
print "Result of $num1 - $num2 is $minus<br /><br />";
print "Result of $num1 * $num2 is $product<br /><br />";
print "Result of $num1 / $num2 is $division<br /><br />";
print "Modulus of $num1 % $num2 is $remainder<br /><br />";
?>
PHP Worksheet 2
2. Average Student Marks
Create an html form that includes a text box labeled student_name and four text
boxes labeled Mark1, Mark2, Mark3, and Mark4 [with no spaces inside the
textbox name].
When the user enters his or her name and four marks, use a PHP script
to return a page with:
- the user’s name
- the average mark (name plus value)
i.e. you must post the values entered to the front end html file to the backend php file
In a similar manner of part 1 previously:
- using the action value in the Form tag to specify the name of your backend php
file
- using $_POST["textboxname"] to receive the value input into the textbox named
and assign to a php variable to use within the php program. (You will need a
separate $_POST[ ] for each of the 5 textboxes used in the frontend html file)
N.B. When creating programs of any complexity it is advisable to build the program in stages and
check for correctness at each stage so:
- Start by creating the web page with 5 textboxes
- Add the post to a backend php file and receive the 5 values
- Use a simple debug statement to print the 5 received values to check if they have
been correctly received
- Then add the calculation of the average plus the final print of the result
ENTER VALUES:
Student Name:
Mark 1:
Mark 2:
Mark 3:
Mark 4
marks.html
SUBMIT
marks.html
<html><body>
<h1> Student Mark Average</h1><br />
<h1> ENTER VALUES</h1><br />
<form action="marks.php" method="post" >
Student Name: <input type="text" name="name"><br /><br />
Mark 1: <input type="text" name="mark1"><br><br>
Mark 2: <input type="text" name="mark2"><br><br>
Mark 3: <input type="text" name="mark3"><br /><br />
Mark 4: <input type="text" name="mark4"><br /><b />
<input type="submit" value="SUBMIT">
</form></body> </html>
marks.php
<?php
// receive values from html webpage & assign to variables in program
$sname = $_POST["name"];
$mark1 = $_POST["mark1"];
$mark2 = $_POST["mark2"];
$mark3 = $_POST["mark3"];
$mark4 = $_POST["mark4"];
//debug print values to check they have been correctly received
print "student name is $sname<br />";
print "mark 1 is $mark1<br />";
print "mark 2 is $mark2<br />";
print "mark 3 is $mark3<br />";
print "mark 4 is $mark4<br /><br />";
// calculate & print the average mark
$avg = ($mark1 + $mark2 + $mark3 +$mark4)/4;
print "average mark is $avg";
?>
3. Create an html form to us the substring function [ substr( ) ] that includes:
- a textbox labeled Word
- a textbox labelled Start_Position
- a textbox labelled Num_Characters
- a Submit button.
This form will allow a word to be entered plus numbers for the Start Position & Num Characters
to be extracted (to be used in substr( ) function) and passed to a backend php program which
will:
- receive the Word, Start_Position & Num_Characters values
- calculate the number of characters in the Word - using strlen( ) – then print the value
with some words of explanation.
- create a new word from the posted Word using the Start_Position &
Num_Characters values posted from the form - using substr( ).
- Print the new word with some words of explanation.
N.B. The Start_Position cannot be higher than the number of characters in the word (minus 1)
[N.B. remember that the Start_Position of 0 is the first character, Start_Position of 1 is the
second character etc]
substring.html
<html><body>
<h1> Generalised Substring Program</h1><br />
<form action="substring.php" method="post" >
Enter word <input type="text" name="word"><br /><br />
Enter start position - 0 or more <input type="text" name="startpos"><br /><br />
Enter number of characters required <input type="text" name="numchar"><br /><br />
<input type="submit" value="Click To Submit">
</form></body> </html>
substring.php
<?php
$word = $_POST["word"];
$startpos = $_POST["startpos"];
$numchar = $_POST["numchar"];
$res = substr($word, $startpos, $numchar);
print "word from $word<br />";
print "start position is $startpos<br />";
print "number of characters is $numchar<br />";
print " substring is $res";
?>
WORKSHEET 3 SOLUTIONS
1. Mark Checker
<?php
$mark = 50;
if ($mark < 0 || $mark> 100) {
print "$mark is an illegal value - enter a number between 0 & 100";
}
else if ($mark >= 0 && $mark <40) {
print "You have failed";
}
else {
print "You have passed";
}
?>
a. Modify the program to include additional tests as follows:
If $mark is





greater than or equal to 70, print "You got a grade A".
at least 60 and less than 70, print "You got a grade B".
at least 50 and less than 60, print "You got a grade C".
at least 40 and less than 50, print "You got a grade D"
less than 40, print "You got a grade E".
b. Create an HTML page to enter a mark to be processed by this PHP script containing:


a form with a POST method to call the above PHP script
a labelled textbox to enter a mark & a submit button
markcheck.html
<html><body>
<h3>Enter a mark between 0 & 100 for checking</h3><br />
<form action="markcheck1.php" method="post">
MARK <input type="text" name="mark"><br /><br />
<input type="submit" value="Submit">
</form>
</body></html>
markcheck1.php
<?php
$mark = $_POST["mark"];
if ($mark < 0 || $mark> 100) {
print "$mark is an illegal value - enter a number between 0 & 100";
}
else if ($mark >= 70) {
print "You got a grade A";
}
else if ($mark >= 60 && $mark < 70) {
print "You got a grade B";
}
else if ($mark >= 50 && $mark < 60) {
print "You got a grade C";
}
else if ($mark >= 40 && $mark < 50) {
print "You got a grade D";
}
else if ($mark < 40 ) {
print "You got a grade E";
}
?>
2. A web form using radio buttons
a. Using PHP to create a web-based membership application form which should include a text
box labelled name and a set of radio buttons that allows the user to select one of the following
age groups:
under 18
18-64
and 65 plus
The form should also include a button to submit the form and a reset button. The receiving program
should do the following:
 print a message if no name is supplied (using the null string ""), asking the user to try
again.
 output a message indicating that the user is too young to become a member if the option
under 18 is checked.
 output a message indicating that the user must pay standard fees if the option 18-64 is
checked.
 output a message saying that the user can receive a 10% discount on fees if the 65 or
older option is checked.
Remember that radio buttons have the general form of:
<input type="radio" name = "cbname" value ="option1" > option1
<input type="radio" name = "cbname" value ="option2" > option2
<input type="radio" name = "cbname" value ="option3" > option3
(If button is clicked, value selected is passed through "cbname" which should be received by
$_POST)
membership.html
<html><body>
<h3>Membership Application - Enter a name and click an age range</h3><br />
<form action="membership.php" method="post">
Name <input type="text" name="name"><br /><br />
<input type="radio" name = "range" value ="a" > under 18
<input type="radio" name = "range" value ="b" checked > 18 - 64
<input type="radio" name = "range" value ="c" > 65 plus
<input type="submit" value="Submit">
</form>
</body></html>
membership.php
<?php
$name = $_POST["name"];
$age = $_POST["range"];
If($name == ""){
print "You must enter a valid name";
}
else {
if($age == "a"){
print "user is too young to become a member";
}
if($age == "b"){
print "user must pay standard fees";
}
if($age == "c"){
print "user can receive a 10% discount on fees";
}
}
?>
3. Simple Calculator
Now create a web page with a form containing two textboxes to enter two numbers plus a
selectbox (drop down list) to select one of four mathematical operations (add, subtract,
multiply & divide).
Remember that a select box has the general form of:
<select name= "listname" >
<option value = "value1" >value1</option>
<option value="value2" >value2</option>
<option value="value3" >value3</option>
……………………………………………
</select>
(selected option value is passed through "listname" – that is what $_POST should receive
The option value does not have to be the same as the value displayed in the list)
The selectbox should have 4 displayed operation values - add, subtract, multiply & divide
with option values corresponding
Use an if statement to check if a textbox value has been entered (i.e. if posted value =="" )
Use further if statement to check if the value entered is a number (using is_numeric( ) which
is true if the value is a number or a numeric string and false if it is not – remember that
!is_numeric( ) means not true ) with appropriate statements to the user to modify input
Use an if … elseif statement to check on the posted listbox values assigned to variables and
- if add then add the variables (+) and print the result
- if subtract then subtract the variables (-) and print the result
- if multiply the variables (*) and print the result
- if divide then divide the first variable by the second (/) and print the result
calc_front.html
<html><body>
<h1> Calculator - Enter two numbers & choose an operation</h1><br />
<form action="calc_back.php" method="post" >
Enter first number <input type="text" name="number1"><br /><br />
Enter second number <input type="text" name="number2"><br /><br />
<select name= "operation" >
<option value="add" >add</option>
<option value="subtract" >subtract</option>
<option value="multiply" >multiply</option>
<option value="divide" >divide</option>
<option value="remainder" >remainder</option>
</select>
<input type="submit" value="Click To Submit">
</form></body> </html>
calc_back.php
<?php
// receive webpage values and assign to program variable
$num1=$_POST["number1"];
$num2=$_POST["number2"];
$op=$_POST["operation"];
// check if either textbox has no value entered
if(($num1 == "") || ($num2 == "")) {
print "you must enter a value in both boxes";
}
//check if either textbox has a non numeric value
elseif((!is_numeric($num1)) || (!is_numeric($num2))){
print "you must enter a numeric value in both boxes";
}
// if textboxes OK then use nested set of if statements to check the operation
// that has been selected and perform appropriate calculation
else{
if($op == "add") {
$result = $num1 + $num2;
}
elseif($op == "subtract") {
$result = $num1 - $num2;
}
elseif($op == "multiply") {
$result = $num1 * $num2;
}
elseif($op == "divide") {
$result = $num1 / $num2;
}
elseif($op == "remainder") {
$result = $num1 % $num2;
}
// print the numbers input with the operation selected and the final result
print "number1 is $num1<br />";
print "number2 is $num2<br />";
print "operation is $op<br />";
print "result is $result";
}
?>
Download