In: Computer Science
Create a web page using PHP and HTML that contains a list of movie names available for rent (at least 10 movies), so that each time you visit the home page, you see a different order of movies in the list. The movie names are required.
2) The rental price for each movie ranges between $1.99 to $7.99.
• Create an associative array with a code for the movie as the key (e.g., movie1, movie2, etc.) and the associated rental price as the value. Display the array using the print_r() function.
• Sort the array according to the rental price in ascending order. Display the array again using the print_r() function.
• Calculate and display the average rental price for a movie.
• Find and display the two highest and two lowest rental prices.
Display these results on the same page. Again, display appropriate warning/error messages as needed.
Hi,
Save the following code in .php file. Most of the code is readable and well commented for better understanding.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Movies For Rent</title>
</head>
<body>
<?php
/*
Create an associative array with a code for the movie as the key (e.g., movie1, movie2, etc.) and the associated rental price as the value. Display the array using the print_r() function.
*/
/*Movies Associate Array*/
$movies = array(
"movie1" => 7.45,
"movie2" => 3.50,
"movie3" => 2.50,
"movie4" => 4.25,
"movie5" => 1.50,
"movie6" => 6.00,
"movie7" => 2.70,
"movie8" => 5.75,
"movie9" => 6.10,
"movie10" => 3.80,
"movie11" => 2.75
);
/*Printing as it is using print_r and true for pretty print*/
echo "<h4>Movies</h4>";
echo "<pre>" . print_r($movies, true) . "</pre>";
?>
<hr>
<?php
/*
Sort the array according to the rental price in ascending order. Display the array again using the print_r() function.
*/
/*Sorting by value [price] using """asort"""*/
asort($movies);
/*Printing after sorting using print_r and true for pretty print*/
echo "<h4>Movies sorted by rental prices</h4>";
echo "<pre>" . print_r($movies, true) . "</pre>";
?>
<hr>
<?php
/*Calculate and display the average rental price for a movie.*/
$total_value_of_movies = array_sum($movies); //sum of all values in the associated array
$movies_count = count($movies);
$average = $total_value_of_movies / $movies_count;
echo "<p>Average Movie Rental Price: <strong>" . round($average, 2) . "$</strong></p>"
?>
<hr>
<?php
/*
Find and display the two highest and two lowest rental prices.
Display these results on the same page.
*/
$rental_prices = array_values($movies); //getting only rental prices that too already sorted
//Displaying last two values of the prices array
echo "<p>Top Two Highest Rental Prices: <strong>" . $rental_prices[$movies_count - 1] . ", " . $rental_prices[$movies_count - 2] . "</strong></p>";
echo "<br>"; //for new line
//Displaying first two values of the prices array
echo "<p>Top Two Lowest Rental Prices: <strong>" . $rental_prices[0] . ", " . $rental_prices[1] . "</strong></p>";
?>
</body>
</html>
Hope this helps.
All the best.