Templating and Decision Making with PHP

This assignment builds upon the previous two by extending the capabilities of your current site with the ability to propgate a standard template across your entire website. It introduces the concept of PHP includes that will allow you to compartmentalize various sections of your site, particularly the header and the footer. We will also add a side menu to further enhance the site. Finally, we will introduce for loops and/or while loops as a simple logic to read in data and output data as a precursor to the next assignment. When you’re done, you’ll be on your way to building your very own CMS (Content Management System). We will elaborate on this in class.

The PHP Include Tags 

Creating a Template Before you start, you should copy (not move) all of your assignment 1 code into a backup folder and call it whatever you want. Your first task is to dissect your site into four parts. The header, the footer, the new side menu and finally the content of your site. This will form the template of your site. Do not confuse this with Joomla or WordPress Templates. Although, they do offer similar functionalities but use an entirely different templating engine called Smarty. The first part is the header which will include all the code that encompasses the header on your site.

Templating and Decision Making with PHP


I have provided a solution for you for assignment 1, so you can match it, or use your own. Since we are creating a menu, we will need to create a side menu for our site. This means that we’ll have to create a new div with the id=”menu” that aligns to the left of the “main” content of our site. This can be easily done with a few css styles. The next step is to make sure that the content starts immediately to the right of the menu. You should end up with something like this where the left side is the menu and the right is the content. Copy all of the links of the assignments into the is section. Test your site to make sure this is correct and then move on to the next step.

Complex Logic Using Conditionals and Loops 

Background Most of not all modern APIs use JSON as the data object that is passed from one site to the next. To read a little more about JSON go here: https://www.w3schools.com/js/js_json_intro.asp. Essentially it is a data object of key-value pairs, for example: name: “John”, age: 31, city: “New York”

In this example “Jon” is the value of the key: name and 31 is value of the key age and so on.

Your task is to create a php page that will process a json string posted from a simple web form with a Text Area Control and a Submit button. After reading in the json object, you will output a graphical presentation of the objects. This can be anything you want. The most basic form will be to create a table that has a row for each element and a column for the key and a column for the value. Using the example above, you should have something like this:  Templating and Decision Making with PHP

Name John Age 31 City New York

Taking it one step further, JSON objects can be nested and have values that are json objects themselves, so you can have something similar to this


“age”:30, “cars”: “car1″:”Ford”,

Page 3

“car2″:”BMW”, “car3″:”Fiat” Which should out put the following

Name John Age 30 Cars Car1 Ford

Car2 BMW Car3 Fiat

Finally JSON Objects can contain arrays as well and looks something like this:

“name”:”John”, “age”:30, “cars”:[ “Ford”, “BMW”, “Fiat” ] Your expected output should look something like this

Name John Age 30 Cars Ford

BMW Fiat

I will be testing with a variety of different variations to confirm that the algorithm you have implemented works correctly. This page shall go into your assignment 3 folder and incorporate the same includes we used in the previous section. In other words the page shall have the same basic layout as the other pages of the site through the use of PHP includes.  Templating and Decision Making with PHP

The criteria for this section is as follows:

  1. You are to use conditionals and iterators to implement the logic. 2. You are not allowed to use external PHP libraries or built in functions to solve the problem. 3. All key value pairs must be in the output. 4. All objects must be represented either as a table within a table or specified as such if you are not using the table

format. 5. I will on be testing the above cases and anything beyond that is out of scope for this assignment.

Your code should work with the associated file that is uploaded with this Assignment.

Templating and Decision Making with PHP