Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4 . Review the index.php page. The details of each book are hardcoded here in HTML . What if we want to add additional books

4. Review the index.php page. The details of each book are hardcoded here in HTML. What if we want to add additional books to the bookstores offerings? Wed have to copy and paste HTML code. This could be done better with a class and book objects.
5. Create a new file called books.php. Use an include_once statement to import this file to index.php (right under line 11 where cart.php is included.) Dont forget to write tags in your new file!
6. In the books.php file declare a class called Book that will allow us to easily add new books.
a. Include public properties (variables inside a class) for:
i. Title
ii. Author
iii. Blurb
iv. Image location
v. Price
b. Add a constructor that accepts 5 input parameters (one for each property). Use it to assign values on creation to those properties. (See my note V for an example.)
7. Now we need to use our new class to create book objects.
a. Using index.php as a reference (or view the page in a browser), create 4 book objects 1 for each of the 4 books listed on the index page. Copy/paste the title, author, blurb, image location, and price for each into your new objects. One of the authors isnt listed, so pass a blank string ("") instead.
i. Image location should be a relative path to the image files (for example: "assets/imgs/learningphp.jpg")
ii. Dont include the dollar sign in the price! Youll see why later.
b. Add each book to an array called $books as you create it; this will help us keep track of each book as we add it and make it easier to add new books later. (The array can be indexed or associative, whichever you prefer.)
c. Use a print_r statement to test if your code is working. Once youre sure everything works, comment this line out so it doesnt interfere with the page.
8. Now we need to use our new array of objects to simplify the page. Open index.php and find where the book section elements are written (around line 21).
a. Make sure you are including your new books.php file on the index page! (see step 5)
b. Replace the 4 html section elements with a loop that outputs the same thing.
i. Loop through the books array with a php foreach...as loop. Inside the loop, print out an html section element for the book. I recommend using copy/paste to get the code correct.
ii. Wherever the books title, author, blurb, image path, and price are output, replace these with references to the book objects property. (Title and price are output twice!)
iii. Hints:
1. If you are using double-quoted strings, be sure to use the backslash (\) character to escape all the quotes in the output text.
2. Remember to delete or comment the original HTML code so you dont print each book twice.
3. Remember to put your PHP code inside tags.
iv. View and test the index page in a browser to confirm it looks correct. Make sure the Add to Cart buttons are still working correctly.
9. Always use a webserver to serve your site as you edit pages so you can view the page and confirm everything is working!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions