WebProgramming - Exercises
WebProgramming - Exercises
1. Create a PHP script to handle a contact form with “name”, “email”, and “message” fields.
Validate the inputs and display success or error messages.
Requirements:
2. Create a form that verifies if a user is over 18 by asking for their birthdate. Calculate the age
based on the current date, validate it, and display whether they are allowed to proceed.
3. Create a PHP script to process an order for different grocery items: “Apples”, “Bananas”, and
“Oranges”. Each item should have a specific price defined as a constant. The script should
display the quantities ordered, calculate the subtotal, and then apply a 5% sales tax to show the
total amount.
4. Write a PHP script to process an order for bakery items: “Bread”, “Pastries”, and “Cookies”.
Retrieve the quantities from a form, calculate the total price based on predefined item prices, and
apply a 15% discount if the total exceeds a certain amount (e.g., $50). Display the original total,
discount amount, and final total.
5. Create a PHP script that calculates the cost of ordering various electronics: “Laptops”,
“Tablets”, and “Headphones”. Define constants for each item’s price, then retrieve quantities
from a form. Calculate the total cost before and after applying a 10% shipping fee. Display an
order summary including each quantity, subtotal, shipping fee, and final total.
6. Build a PHP script that simulates drawing six unique numbers between 1 and 49 (like a lottery
draw). Sort the numbers in ascending order and check for specific outcomes:
Hint: Use “rand()” and “sort()” for generating and organizing the numbers, then apply logic to
check for consecutive values.
7. Write a PHP script that simulates a slot machine. Generate three random symbols (1-5) and
display each symbol in an image format. Award winnings based on the following:
Hint: Use an array to store the random symbols and “array_count_values()” to count
occurrences.
8. Create a PHP script that takes a user’s birth year (inputted from a form) and calculates their
age. Display the age and a message based on the age group:
Hint: Use “date("Y")” to get the current year and subtract the birth year.
9. Create a PHP script that accepts three test scores as input and calculates the average. Display
the average score and the corresponding grade based on this scale:
- 90-100: Grade A
- 80-89: Grade B
- 70-79: Grade C
- 60-69: Grade D
$fruit_prices = array(
);
Write a PHP script that calculates the total cost of purchasing one of each fruit and displays the
total price.
Sample Output:
11. Define an associative array with student names as keys and their grades as values:
$student_grades = array(
"Doe" => 78
);
Create a PHP script that calculates the average grade of the students and displays each student's
name along with their grade.
Sample Output:
John's grade: 85
Jane's grade: 92
Doe's grade: 78
Average grade: 85
12. Create an associative array where the keys are image filenames and the values are their
descriptions:
$image_gallery = array(
);
Write a PHP script to display each image along with its description. Ensure to use the “<img>“
tag to display the images.
$product_catalog = array(
);
Create a PHP script that displays each product and its price, and calculate the total price of all
products.
Sample Output:
Create a MySQL database named library. Include the following tables: users (with
columns: id, username, password, email, role), books (with columns: id, title, author,
publication_year, genre_id), and genres (with columns: id, name).
Write a separate PHP script for the database connection, named “db_connect.php”, which
establishes a connection to the MySQL database and handles any connection errors.
Write a PHP script that checks if a user exists in the “users” table. If the user exists and
the password matches, return true; otherwise, return false. Use “mysqli” queries for the
checks.
Write a PHP script that retrieves all books from the “books” table belonging to the
specified genre ID. Execute a “mysqli” query to return the results and show them on
separate rows.
Write a PHP script that inserts a new book into the “books” table using a “mysqli” query.
The script should take the title, author, publication year, and genre ID as input.
Write a PHP script that updates the details of a book in the “books” table based on the
provided book ID. Use a “mysqli” query for the update operation.
14.6. Delete User Account
Write a PHP script that deletes a user from the “users” table based on the provided user
ID. Ensure that the user exists before attempting to delete and return a success or failure
message. Use a “mysqli” query for the deletion.
15.1 Assume there is a form with a text input field named “username”. Start a session at
the beginning of your PHP script. Retrieve the username from the form submission and
store it in a session variable called “username”. After storing the username, display a
confirmation message showing the stored name.
15.2 Retrieve and display data stored in a session. Start a session at the beginning of your
PHP script. Check if the session variable “username” is set. If it is set, retrieve the
username from the session and display a welcome message with the username. If it is not
set, inform the user that no name is stored in the session.
15.3 Destroy a session and clear stored session data. Start a session at the beginning of
your PHP script. Check if the session variable “username” is set. If it is set, display the
current username. Then, clear all session variables using the appropriate session functions
and destroy the session. Display a message confirming that the session has been
destroyed.
16. Assume there is a form with a text input field named “favorite_color”. Start a session at the
beginning of your PHP script. Retrieve the favorite color from the form submission and store it
in a session variable called “favorite_color”. After storing the favorite color, display a
confirmation message. Then, check if the session variable is set and display the stored favorite
color. Clear the favorite color from the session and confirm that it has been removed.
17. Create a session counter that tracks how many times the user has visited a page. Start a
session at the beginning of your PHP script. Check if a session variable called “visit_count” is
set. If it is set, increment the count; if it is not set, initialize it to 1. Display the number of times
the user has visited the page. Provide a link or option to reset the counter, which clears the
session variable.