0% found this document useful (0 votes)
10 views

PHP Assignment

Uploaded by

mnmengwai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

PHP Assignment

Uploaded by

mnmengwai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

RICHFIELD GRADUATE INSTITUTE OF TECHNOLOGY (PTY)

LTD
FACULTY OF INFORMATION TECHNOLOGY

INTERNET PROGRAMMING 621


ST
1 SEMESTER ASSIGNMENT

Name & Surname: Mengwai Mphela


Neo_________________________________ ICAS No: _________________
Qualification: _DIT Semester:1 Module Name: Internet programming
3YRS_____________________ _____ 621__________________________
Date Submitted: 9 May
2019___________

ASSESSMENT CRITERIA MARK EXAMINER MODERATOR


ALLOCATION MARKS MARKS
MARKS FOR CONTENT
QUESTION ONE 50
QUESTION TWO 50

TOTAL MARKS 100


Examiner’s Comments:

Moderator’s Comments:

Signature of Examiner: Signature of Moderator:

1
TABLE OF CONTENTS PAGE#
Question 1

1.1 / 3-4

1.2 \ 4-5

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

Question 2

2.1 / 5-7

2.2 \ 7-10

2.3 / 10-11

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

2
Question 1

1.1

Screenshots:

Source Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>Question 1.1</h3>
<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">
<?php
for($row=1;$row<=8;$row++)
{
echo "<tr>";
for($col=1;$col<=8;$col++)

3
{
$total=$row+$col;
if($total%2==0)
{
echo "<td height=30px width=30px bgcolor=#FFFFFF></td>";
}
else
{
echo "<td height=30px width=30px bgcolor=#000000></td>";
}
}
echo "</tr>";
}
?>
</table>
</body>
</html>

1.2

Screenshot:

Source Code:

<?php
echo 'Question 1.2';
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$p = $col * $row;
echo "<td>$p</td> \n";
}

4
echo "</tr>";
}
echo "</table>";
?>

Question 2

2.1
Screenshot:

Source Code:

<!DOCTYPE html>
<html>
<body>
<table border = "1rem" cellpadding="5px">
<?php
$cars = array
(
array(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2)

5
) ,
array(
array("Land Rover",17,15),
array("Range Rover",16,14),
array("Nissan",27,25)
),
array(
array("Hundai",8,3),
array("Kia",7,1),
array("Jeep",27,15),
array("Benz",8,5)
)
);

echo '<th>Brand name</th>';


echo '<th>Number of stock in Store</th>';
echo '<th>Quantity of stock</th>';

for($l=0;$l<3;$l++){
echo "<tr>";
for($r=0;$r<3;$r++){
for($c=0;$c<3;$c++){
echo "<td>".$cars[$l][$r][$c]."</td>";
}
echo "</tr>";
}
}
?>
</table>
</body>

</html>

2.2
WHAT IS A CONTROL STRUCTURE?
Code execution can be grouped into categories as shown below

Sequential – this one involves executing all the codes in the order in which they have been
written.

Decision – this one involves making a choice given a number of options. The code executed
depends on the value of the condition.

6
A control structure is a block of code that decides the execution path of a program
depending on the value of the set condition.

PHP IF Else

If… then... else is the simplest control structure. It evaluates the conditions using Boolean
logic
When to use if… then… else

 You have a block of code that should be executed only if a certain condition is true
 You have two options, and you have to select one.
 If… then… else if… is used when you have to select more than two options and you
have to select one or more

The syntax for if… then… else is;

<?php
if (condition is true) {

block one

else

block two

}
?>

HERE,

 “if (condition is true)” is the control structure


 “block one” is the code to be executed if the condition is true
 {…else…} is the fallback if the condition is false
 “block two” is the block of code executed if the condition is false

Now here’s how it will look like in an actual program :

<?php

$first_number = 7;

$second_number = 21;

if ($first_number > $second_number){

7
echo "$first_number is greater than $second_number";

}else{

echo "$second_number is greater than $first_number";

?>

Output will be : 21 is greater than 7

PHP Switch Case

Switch… case is similar to the if then… else control structure. It only executes a single block
of code depending on the value of the condition. If no condition has been met, then the
default block of code is executed. It has the following basic syntax.

<?php
switch(condition){

case value:

//block of code to be executed

break;

case value2:

//block of code to be executed

break;

default:

//default block code

break;

}
?>

HERE,

8
 “if (condition is true)” is the control structure
 “block one” is the code to be executed if the condition is true
 {…else…} is the fallback if the condition is false
 “block two” is the block of code executed if the condition is false

Now here’s how it will look like in an actual program :

<?php

$today = "wednesday";

switch($today){

case "sunday":

echo "pray for us sinners.";

break;

case "wednesday":

echo "ladies night, take her out for dinner";

break;

case "saturday":

echo "take care as you go out tonight.";

break;

default:

echo "have a nice day at work";

break;

?>

Output : ladies night out, take her for dinner.

Summary

 Control structures are used to control the execution of the program

9
 The if then... else is when you have more than route block of code to execute
depending on the value of the condition
 Switch… case is used to when you have a number of block codes, and you only have
to execute one of them depending on the value of the set case

2.3
Screenshot:

Source code:
<?php
/*The student names*/
$students = ["Sauer Jeppe","Von Weiligh","Troy Commisioner","Paul
krugger","Jacob Maree"];
/* The students marks*/
$marks = [75,44,60,62,70];

switch($marks){
case $marks >= 70 : echo "$students[0] scored a distinction";
echo "<br>";
echo "$students[4] scored a distinction";
echo "<br>";
case $marks >= 50 && $marks<69 : echo "$students[2] scored a pass";
echo "<br>";
echo "$students[3] scored a pass";
echo "<br>";
case $marks < 49 : echo "$students[1] scored a fail";
};

?>

10
11

You might also like