worksheet9

advertisement

DFTI Worksheet 9

Functions and associative arrays

IMPORTANT!

This worksheet is intended only for students have completed the basic questions from all worksheets so far.

Before attempting this worksheet, you should ensure you have completed the basic questions from all previous worksheets. In particular, ensure that Worksheets 6 (query strings), 7

(sessions and logins) and 5 (INSERT statements) are all done before you do this.

Functions

1. In a separate file, write a connect() function to connect to the database, as is done in the lecture notes. Change your HitTastic! code so that it uses this connect() function.

2. In the same file as you used in Question 1, write a write_head() function which writes out your page's <head> section, i.e. its title, and the links to stylesheets. Use it on all your HitTastic! Pages.

3. Change your write_head() function so that it takes two parameters: the page title, and the CSS filename. The write_head() function should echo out the <title> and the CSS link using the two parameters passed into the function.

Associative arrays

4.

If you did not do so in Week 1, give your HitTastic! main index page a layout with a <div> for the sidebar and a <div> for the main content area of the page. A simple way to do this in CSS is as follows:

#sidebar

{

top:0%; left:0%; width:20%; height:100%; position: absolute;

}

#main

{

top:0%; left:20%; width:80%; height:100%; position: absolute;

}

Inside the sidebar, create an associative array to store the links to the other pages (i.e. the signup page, and the admin page to update the quantity of an existing hit – see Week 6). The link titles (i.e. text on the links) should be the keys, and the page to link to, the values.

Use a foreach loop to loop through each entry in the associative array and display a link.

Combining functions and associative arrays

5. Write a write_sidebar() function which writes out a sidebar <div>, as described in the associative arrays exercise, above. It should take an associative array of links (as described in the associative arrays exercise, above) as a parameter, and loop through the associative array, writing out each link within it.

Test it out by adding to your three pages (the home page, the signup page, and the admin page to update the quantity of an existing song), ensuring that each page has a layout with a sidebar <div>. Each page should link to the other two pages.

Download