Class 7 – Assignment #1

Posted: October 31st, 2009 | Author: | Filed under: assignments, php | Tags: , , | No Comments »

Your in-class assignment today is to update your e-commerce pages from the previous class to be more “dynamic” and “templatized”.

Templatize the common sections of all pages in the entire site

Use separate PHP include files for the top ad banner, global navigation, breadcrumbs, skyscraper ad, and footer.  On a real site, these sections would be the more-or-less the same on all pages, so you would want to have them stored in reusable files.  These files will be included into the main XHTML file for each page using the include() function of PHP.  Feel free to check out the include examples that are up on the server.

Note: As a convention in this class, whenever you create a file that has a snippet of XHTML that is included into another page but never displayed directly on its own, I would like you to give it a name prefixed with the “_” character.  For example, the include files used in this assignment could be called:
  • _header.php
  • _breadcrumbs.php
  • _ad_banner.php
  • _ad_skyscraper.php
  • _footer.php
Templatizing these parts of the page will allow you to reuse those sections on multiple pages on your site, if you ever build it out to be more than one page, without having to rewrite the code for each.  Naming the files with this convention will make it obvious when looking at a list of your own files which ones are main XHTML pages and which ones are meant to be include files.

Templatize the products

Once you have finished that, it’s time to start templatizing the actual products that are displayed on the page as well.  This would, in theory, allow you to use the same page template for multiple categories of products.

We will do this in a different way than how we templatized the repeating sections of the page.  We will be using multidimensional arrays this time.  This exercise will be useful as a preparation for when we start to store data in databases.

Eventually, we will want the data for the products to be pulled from a database.  But for now, we’re just going to store data in an array as an intermediary step to that goal.

To templatize the products, create a PHP multidimensional array that contains all of your product data.  And use a PHP foreach loop to loop through that array and display the product data for each product on your page, rather than having it all hard-coded in the XHTML.

Do the tutorials, understand the examples

You are going to have to go through all the PHP tutorials on the Tizag site and my introduction to arrays in order to get a grasp of arrays and multidimensional arrays in particular.

Here is an example of how to get starting using a multidimensional array for this assignment.  I did not show how to use the foreach loop here, so you will have to investigate that, based on the example of looping through an array using the foreach command shown here.

Be sure to keep a backup of the work you did on the e-commerce page in previous classes – do not overwrite it, just make a copy of it.

Links to helpful documentation

No related posts.



Leave a Reply