In: Computer Science
PHP Question:
Subject: Functions and Arrays.
INSTRUCTIONS:
Objective:
• Write functions.
• Use server-side includes.
• Create and utilize a numeric array.
• Create and utilize an associative array.
Requirements:
Create a script file called functions.php, where you will be adding functions.
priceCalc() function:
• 2 parameters: price and quantity.
• Create a numeric array of discounts with the following values:
0,0,.05,.1,.2,.25.
• Get the discount percent from the array using the quantity as the
index. If the quantity is greater than 5, use 5 as the index.
• Calculate the discount price using the discount percent.
• Calculate the total by multiplying the discount price by the
quantity.
• Return the total to the calling script.
Create a new page called invoice.php:
1.) It would be beneficial to make a copy of
the controls.php file and rename it to invoice.php for this
section.
2.) Include the functions.php file.
3.) Create an associative array that uses the
artist name as the key and the album title as the value. Use any
artists and songs that you like and include 10 elements.
4.) Include the header.php file and passing the
pageTitle variable to it.
5.) Add a separate entry to the array for "The
White Album" by "The Beatles" bringing the total number of albums
to 11.
6.) Create a heading variable and assign it the
value <h2> Artists and Albums </h2>.
7.) Use a foreach loop to write out every artist
and album from the associative array to the web page.
Using the while and for loops from the Controls assignment:
1.) In each loop, replace the price calculation
with a call to the priceCalc() function, passing the quantity equal
to the loop counter and a price of 12.99 if the download variable
is false or 9.99 if it is true. Write out the quantity and total
for each iteration.
2.) Add the shipping if download is false.
3.) Include footer.php
There are few points that I want to mention before the solution: Please go through the following points
1. There is no controls.php file provided in the question.
2. For the while and for loops from the Controls assignment
section:
a. There is no limit for iteration mentioned, so I
have taken it to be 11, number of albums
b. And no download variable value is provided and no
condition as when it is going to be true . So I have done the
program taking it to be false.
c. No shiping price is given , so I have taken it to
be 20 by default.It can be changed according to your requirement
which is not mentioned in the question.
functions.php
<html>
<head>
<title>Calculate the total price</title>
</head>
<body>
<?php
function priceCalc($price, $quantity) {
$discounts= array(0.0,0.0,0.05,0.1,0.2,0.25);
$index=$qunatity;
if(&quantity>5)
$index=5;
$discountPrice= $price-(($discounts[$index]*$price)/100);
$total= $discountPrice* $quantity;
return $total;
}
?>
</body>
</html>
invoice.php
<html>
<head>
<title>Albums Collection and their price</title>
</head>
<body>
<?php include 'functions.php';
$pageTitle = 'Get the collections along with their total price';
include 'header.php';
include 'footer.php';
$albums=array("A Girl Called Eddy"=>"Been Around", "Halsey"=>"37", "Manic"=>"43","Little Big Town"=>"Nightfall","Mac Miller"=>"Circles","Mura Masa"=>"R.Y.C.","Of Montreal"=>"Ur Fun","Pinegrove"=>"Marigold","Prince Royce"=>"Alter Ego","Beach Bunny"=>"Honeymoon");
$albums["The Beatles"]="The White Album";
$heading= "<h2> Artists and Albums </h2>";
echo $heading;
foreach($albums as $x => $x_value) {
echo "Artist: " . $x . ", Albums: " . $x_value;
echo "<br>";
}
$download=false;
$shipping=20;
for ($x = 0; $x <= 11; $x++) {
if($download== false)
$price=12.99;
else
$price=9.99;
$total=priceCalc($price,$x);
if($download==false)
$total=%total+$shipping;
echo "Quantity:" .$x. ", Total Price: " .$total;
echo "<br>";
}
?>
</body>
</html>