This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Introduction to PHP

Assignments for Week #6 - Conjunction Junction, What's My Function?

Matthew Buscemi's picture
Mon, 2011-02-28 19:53

1) Your boss instructed you to build a page that will accept an array of book information and then display those books, their titles, associated images, and price, and a form for buying (but only if the book is actually in stock!). You complete your work successfully (http://codepad.org/4LpX1z6J), the page works beautifully, and you begin to lean back and enjoy your success. Then...

  1. Well, it turns out that Frank, that guy from accounting, he made a PHP page for all the sales figures and that the sales tax is actually 1.065, not 1.055 as you were told. Your pages and his pages are all generating different prices and the database is a big mess. Make a function for generating the after-tax price that you and Frank can share to ensure that both pages determine the price of books the same way.
  2. Next, you discover that the newbie software developer just out of college four desks down made his own method for book titles with initial articles, and he made a mess of it.  His code takes "A Wizard of Earthsea" and turns it into "ard of Earth, Asea".  Help him out by turning your code for title modification into a function, so that you can share it with him.
  3. Those book images are almost certainly going to end up being used on multiple pages.  Let's put the code that generates the image paths into a function too!
  4. Figuring out if a book is currently in stock is going to be a common function too.  Even though it's simple functionality, the more code you put in your function list, the more you can ensure that methods will not be duplicated across your system.  Write a function that returns "true" if the book is in stock and "false" otherwise.  Such a function, which returns boolean "true" or "false", can be used inside an if() statement!
  5. The sales team wants to get the author's name printed on the page you made.  Make a new div with class "author" somewhere inside <div class="book-container">.  The sales team wants the author's name printed like this: "[Last Name], [First Name]".  But, the array is populated from a database, which has only "[First Name] [Last Name]".  Modify the page to include these changes.  Make sure that your code for converting the author's name is in a function so that you can use it on any new pages you make and share it with the rest your team.