Answered step by step
Verified Expert Solution
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
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.
Create a new file called books.php Use an includeonce statement to import this file to index.php right under line where cart.php is included. Dont forget to write tags in your new file!
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 input parameters one for each property Use it to assign values on creation to those properties. See my note V for an example.
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 book objects for each of the books listed on the index page. Copypaste 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: "assetsimgslearningphpjpg
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 printr statement to test if your code is working. Once youre sure everything works, comment this line out so it doesnt interfere with the page.
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
a Make sure you are including your new books.php file on the index page! see step
b Replace the 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 copypaste 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:
If you are using doublequoted strings, be sure to use the backslash character to escape all the quotes in the output text.
Remember to delete or comment the original HTML code so you dont print each book twice.
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.
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started