Question

In: Computer Science

PHP Question: Subject: Functions and Arrays. INSTRUCTIONS: Objective: • Write functions. • Use server-side includes. •...

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

Solutions

Expert Solution

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>



Related Solutions

PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML...
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML form. • Submit form for processing. • Sanitize and validate form data. • Upload a profile image. • Add a record to a text file. • Display form results with content from another text file and the uploaded image. Description: This assignment deals with file management. It requires the use of 3 new files and 1 new directory. You will create the new file-uploads.php...
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML...
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML form. • Submit form for processing. • Sanitize and validate form data. • Upload a profile image. • Add a record to a text file. • Display form results with content from another text file and the uploaded image. Description: This assignment deals with file management. It requires the use of 3 new files and 1 new directory. You will create the new file-uploads.php...
PHP Question: Subject: Managing Persistence. Objective: • Create a login script. • Strengthen passwords. • Authenticate...
PHP Question: Subject: Managing Persistence. Objective: • Create a login script. • Strengthen passwords. • Authenticate users. • Create sessions. • Create cookies. • Manage persistence. Description: All dynamic sites need a way to manage access to resources. Many dynamic sites use back-end databases that can contain sensitive information that needs to be securely managed. At the same time, sites want to be inviting to guests. Managing the access to sensitive information is a critical function. Requirements: Update site for...
PHP    1. Create a new PHP document (nit Objective 1)    2. Write a comment...
PHP    1. Create a new PHP document (nit Objective 1)    2. Write a comment similar to the following: "This is my first PHP document, which displays some data in the web browser"(Unit Objective 1)    3. Assign your name as a string value into the variable labeled myName (Unit Objective 2)    4. Assign 53870 numeric value into the variable named randomNumber (Unit Objective 2) 5. ssign the name of the web browser and operating system of the...
PHP Question: Write the PHP code to list out all of the dates of the current...
PHP Question: Write the PHP code to list out all of the dates of the current month and assign them to their given days. As an example, for the month of October 2020, the output should look something like this: October 2020       Monday: 5, 12, 19, 26 Tuesday: 6, 13, 20, 27 Wednesday: 7, 14, 21, 28 Thursday: 1, 8, 15, 22, 29 Friday: 2, 9, 16, 23, 30 Saturday: 3, 10, 17, 24, 31 Sunday: 4, 11, 18,...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and...
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.   Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. Create GetMiles() function that returns...
Create a new PHP document (Unit Objective 1) Write a comment similar to the following: "This...
Create a new PHP document (Unit Objective 1) Write a comment similar to the following: "This is my first PHP document, which displays some data in the web browser"(Unit Objective 1) Assign your name as a string value into the variable labeled myName (Unit Objective 2) Assign 53870 numeric value into the variable named randomNumber (Unit Objective 2) Assign the name of the web browser and operating system of the user accessing the file into the variable named userAgent (Unit...
Create a new PHP document (Unit Objective 1) Write a comment similar to the following: "This...
Create a new PHP document (Unit Objective 1) Write a comment similar to the following: "This is my first PHP document, which displays some data in the web browser"(Unit Objective 1) Assign your name as a string value into the variable labeled myName (Unit Objective 2) Assign 53870 numeric value into the variable named randomNumber (Unit Objective 2) Assign the name of the web browser and operating system of the user accessing the file into the variable named userAgent (Unit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT