Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Lab_3..pdf
1. ST. CLAIR COLLEGE @ ACE ACUMEN ACADEMY
COMPUTER SYSTEMS TECHNICIAN – NETWORKING
PHP
Lab III
Instructor
MANDEEP
KAUR
Section(s)
MIT 439 043
September 25, 2023
2. $book1 = array(
"title" => "Before the Frost",
"author" => "Henning Mankell",
"year" => 2002,
"genre" => "Crime Novel"
);
PHP Lab III
Objective
Create and manage a simple book catalog for a library using arrays and foreach loops.
Requirements
1. Create an associative array to represent a book. Each book should have:
• A title
• An author
• A publication year
• A genre
2. Create a multidimensional array to store multiple books.
3. Display the entire catalog, including details using a foreach loop.
4. Create a function to display books of a specific genre.
Procedure
1. Defining a Book
Define an associative array to represent a single book.
2. Creating a Catalog (40 Pts)
Using the book structure above, create a library catalog as an array of books.
Eduardo Nava 1
3. $library = array(
array(
),
array(
),
# Add 10 more books. Make sure to include at least 2 books of the same genre.
# Also, recall that the array() construct is not the only way to create arrays.
);
foreach ($library as $book) {
echo "Title: " . /* your code here */ . "<br>";
echo "Author: " . /* your code here */ . "<br>";
echo "Year: " . /* your code here */ . "<br>";
echo "Genre: " . /* your code here */ . "<br><br>";
}
PHP Lab III
"title" => "Before the Frost",
"author" => "Henning Mankell",
"year" => 2002,
"genre" => "Crime Novel"
"title" => "1984",
"author" => "George Orwell",
"year" => 1949,
"genre" => "Dystopian"
Notice: It would probably be a bad idea to hard-code so many books in a production environment.
Nonetheless, this lab aims to familiarize you with the array syntax and, thus, the repetition.
3. Displaying the Catalog (10 Pts)
Complete the following lines of code. You will use a foreach loop to iterate through the library catalog
and display each book’s details.
Eduardo Nava 2
4. function displayByGenre($genre) {
global $library;
foreach ($library as $book) {
if ($book["genre"] == $genre) {
# Your code here
/* Complete the foreach loop to display the book's information.*/
}
}
}
# Invoking the function:
displayByGenre("Dystopian");
PHP Lab III
4. Display Books by Genre (20 Pts)
Create a function that takes a genre as a parameter and displays all books from that genre using a
foreach loop. Notice this task requires you to use techniques I have yet to teach you. I will help you
with those. Your only job is to complete the code using what you already know.
4. Next Level (10 Pts)
Add the following attributes to each book in your lab:
• ISBN
• Number of Pages
• Publisher
5. Challenge (20 Pts)
Try to understand the syntax of the function in step 4. Use the same methods to create a fucntion that
displays books published in a specific year.
Eduardo Nava 3