INF335a – PHP Homework 2
Kiril Hadzhyiski 200146294
“I hereby declare that the submitted programming homework solutions are all my
own work. I did not copy the solutions from someone or somewhere else. No one
but me developed the solutions. I have not allowed my solutions to be copied by
someone else.”
Date: 21/02/2022 Signature: Kiril Hadzhyiski
I have completed all exercises and they are working correctly.
Exercise 1: Displaying the contents of a 2D array:
Code:
<?php
$array = array(
'foo' => 2020,
5 => 'Hello World!',
'bar' => array(1, 2, 3, 5, 8, 13)
);
foreach ($array as $key => $value)
{
echo $key.": ";
if (gettype($value) == "array") {
foreach ($value as $arr_value)
{
echo $arr_value." ";
}
}
else
{
echo $value." ";
}
echo "<br>";
}
?>
Exercise 2: Displays the contents of an array with books using the var_dump()
function:
<?php
$books = array(
array(
"title" => "Catch 22",
"director" => "Joseph Heller",
"year" => 1961
),
array(
"title" => "Hitchhikers Guide to the Galaxy",
"director" => "Douglas Adams",
"year" => 1979
),
array(
"title" => "Lord of the Rings",
"director" => " J. R. R. Tolkien ",
"year" => 1968
)
);
echo var_dump($books);
?>
Exercise 3: An HTM form that takes as input an integer in the range 0-9 and
echoes the text form of the number. If the number is invalid it echoes an error
message:
Code:
Number.htm
<html>
<body>
<form action="ConvertNumber.php" method="POST">
<label>Enter an integer in the range 0-9:</label><br>
<input type="text" name="number"><br>
<input type="submit" value="Submit"><br>
</form>
</body>
</html>
ConvertNumber.php
<html>
<body>
<?php
$arr = array("zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine");
$num = $_POST["number"];
if (0 <= $num && $num <= 9)
{
echo $arr[$num];
}
else
{
echo "Input is invalid. Entered number must be in the
range 0-9!";
}
?>
</body>
</html>
Exercise 4: Create a one-part web form implemented with a set of checkboxes
Code:
<form action="" method="post">
Please choose your favourite foods:
<input type="checkbox" name="foods[]" value="pizza">Pizza<br>
<input type="checkbox" name="foods[]" value="burger">Burger<br>
<input type="checkbox" name="foods[]" value="taco">Taco<br>
<input type="submit" value="Submit" />
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['foods'])){
$selectedFoods = $_POST['foods'];
echo "Your selected food items are:<br>";
foreach($selectedFoods as $food){
echo $food . "<br>";
} else {
echo "Please select at least one food item.";
?>
Exercise 6: A form that requires the user to input a 4-digit number and a name. If
the input is invalid it shows an error message and prompts the user to enter the
values again. If the input is valid it echoes a message.
Code:
<?php
$number_val = true;
$name_val = true;
$number = $name = "";
$trigger_number = $trigger_name = false;
if (isset($_POST["submit"]))
{
if (empty($_POST["number"]))
{
$number_err = "Field number is empty!";
}
else
{
$number = clean_input($_POST["number"]);
if (!is_numeric($_POST["number"])) $number_val = false;
if (strlen((string)$_POST["number"]) != 4) $number_val =
false;
if ($number_val == false)
{
$number_err = "Field number is invalid!";
$number = "";
}
else
{
$trigger_number = true;
}
}
if (empty($_POST["name"]))
{
$name_err = "Field name is empty!";
}
else
{
$name = clean_input($_POST["name"]);
if (!preg_match("/^[A-Za-z]{1,50}$/", $_POST["name"]))
$name_val = false;
if ($name_val == false)
{
$name_err = "Field name is invalid!";
$name = "";
}
else
{
$trigger_name = true;
}
}
}
function clean_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<html>
<body>
<form action="" method="POST">
<label>Enter a 4-digit number:</label><br>
<input type="text" name="number" value="<?php echo $number ?
>" required><br>
<label>Enter your name:</label><br>
<input type="text" name="name" value="<?php echo $name; ?>"
required><br>
<input type="submit" name="submit" value="Submit"><br>
<span class="error"><?php if (isset($number_err)) echo
$number_err ?></span><br>
<span class="error"><?php if (isset($name_err)) echo
$name_err ?></span><br>
<span class="success"><?php if ($trigger_number &&
$trigger_name) echo "Your input is valid!" ?></span><br>
</form>
</body>
</html>