Test 2/Spring '06 PC-Component Instructions

This portion of your test involves getting a couple of PHP scripts working. It involves two parts: a script modification problem and either a debug or a programming problem. You are allowed to open Notepad, Dreamweaver, Visual Studio, and one or more browsers during the exam. The only sites/pages you may access through the browser windows are:

Please do not open any other applications, web sites, or other files containing PHP code. Opening other applications will result in a grade of "F" for this exam.

This portion of the test is open book/open note, so you may use your notes. You may not use your book or notes for the written part, so do not go any further with this portion of the test until you've turned in your written portion.

Remember to write code that is xhtml compliant. Also, you are responsible for any errors incurred because of problems with cut and paste, so be careful. Remember that some browsers such as IE will add code if you do a "Save As".

Good luck!

Part 1: Modification of Existing Script (25 points)

Below, I have given you the code for an XHTML file containing a PHP script I have written that accesses the SQL database zxyx999. The connection is made through our dummy account 'zxyx999' using password '12345'. It accesses two of the three tables you should be familiar with from our "mylibrary" exercises. Basically, it displays all of the records from the mylibrary table and uses the records from the genres table to get the descriptions of the different genres for output. This avoids having to use the cryptic 'F', 'T', 'P', 'C', and 'A' ID's in the table.

<!-- The code begins after this line -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Output from My Library</title>
</head>
<body>
<?php
    print "<h1 align='center'>Output from &quot;My Library&quot;</h1>\n";

// Connecting to MySQL using the username "zxqx999" and the password "12345"
// IF YOU MODIFY THE USERNAME AND PASSWORD FOR DEBUGGING PURPOSES, BE SURE
// TO SET IT BACK TO THESE VALUES BEFORE YOU SUBMIT IT.
    $connection = mysql_connect ("localhost", "zxyx999", "12345");

// Using the database "zxyx999. IF YOU MODIFY THE DATABASE FOR DEBUGGING
// PURPOSES, BE SURE TO SET IT BACK TO THIS VALUE BEFORE YOU SUBMIT IT.
    mysql_select_db("zxyx999", $connection);

//// Make Array of Genre Types (This method might also work for status!) ////
// From the table genres, grab the fields ID and GENRE_DESC. From these
// records, we will make an array of genre descriptions called $genres.
// By the way, there is a way you can use a relational search to do this
// with the query alone and not using this ugly second query.

    $genre_query = mysql_query("select ID, GENRE_DESC from genres", $connection);
    $genres = array();
    while($genre_record = mysql_fetch_array($genre_query, MYSQL_ASSOC))
        $genres[$genre_record[ID]] = $genre_record[GENRE_DESC];

// From the table library, grab the fields TITLE, AUTHOR, PUB_YR, and GENRE
// for all of the records. The received records should be sorted on the
// field TITLE
    $result = mysql_query("select TITLE, AUTHOR, PUB_YR, GENRE from mylibrary order by TITLE", $connection);

// Start the table in HTML and print the column headings
    print "<table align='center' border='1' cellpadding='3'>\n";
    print "<tr>\n";
    print "<td align='center'>Title</td>\n<td align='center'>Author</td>\n<td align='center'>Publication Year</td>\n<td align='center'>Genre</td>\n";
    print "</tr>\n";
    
// Print the individual records, each as a row in the table
    while($record = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        print "<tr>\n";

        print "<td align='center'>";
        print $record[TITLE];
        print "</td>\n";

        print "<td align='center'>";
        print $record[AUTHOR];
        print "</td>\n";

        print "<td align='center'>";
        print $record[PUB_YR];
        print "</td>\n";

        print "<td align='center'>";
        print $genres[$record[GENRE]];
        print "</td>\n";

        print "</tr>\n";
    }

// Close the table tag
    print "</table>\n";

// Close the connection to MySQL
    mysql_close ($connection);
?>
</body>
</html>

<!-- The code ends before this line -->

Your assignment is to modify this code to add the following features:

The final output for your script should look like this:

Output from "My Library"

Title Author Publication Year Genre Status
Road Ahead, The Bill Gates 1996 Technical Renewed
Computer Organization Carl Hamacher 2001 Technical Checked out
Linux Bible, 2005 Edition Christopher Negus 2005 Technical On shelf
How to Win Friends & Infl. People Dale Carnegie 1990 Personal Growth Overdue
Linux For Dummies, 6th Edition Dee-Ann LeBlanc 2005 Technical On shelf
Oh, the Places You'll Go! Dr. Seuss 1990 Childrens On shelf
If You Give a Moose a Muffin L. Joffe Numerof 1991 Childrens Renewed
If You Give a Pig a Pancake L. Joffe Numerof 1998 Childrens Checked out
Cell Stephen King 2006 Audio tapes/CDs Processing
Bag of Bones Stephen King 1999 Fiction On shelf
On Writing Stephen King 2000 Audio tapes/CDs Renewed

In case you want to do the debugging in your own account, i.e., you want to see what the select statements generate in MySQL, the script that I used to generate the mylibrary database can be found at this link. It should be identical to the one you used for the in-class exercise, so if you did that on your account, you should already have the table.

Be careful in your editing. There is already one debugging exercise here, and this one isn't supposed to be it.

When you have completed this portion of the program, send it to me using your digital dropbox in Blackboard. Use the file name <your_last_name>_modify.htm. For example, John Smith would submit the file "smith_modify.htm".

Part 2: Either Debug Existing Code or Write New Code (25 points)

For this last portion of the test, you are to pick one of two options, either a debugging exercise or a coding exercise.

Option 1: Debugging Exercise

I have created a PHP file containing a script to be executed when it receives the output from a form using the get method. It uses the same timetable database table that you used for your in-class exercises where the output is the list of courses for a specific instructor. The data from the form is the instructor's name assigned to the form input "inst". I have used the get method so you will not need a form. Since the get method is used, you can debug it using a simple URL which includes the get data. By simply adding "?inst=laws" or "?inst=tarnoff" (without the quotation marks) to the end of the URL, you will send the get data of an instructors name in the form variable inst.

First, the file you are to debug is presented below. Copy this code into your editor and save it to your Einstein account under the name <your_last_name>_debug.php. For example, John Smith would submit the file "smith_debug.htm".

<!-- The debug code begins after this line -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Course Section Search Based on Instructor Name</title>
</head>
<body>
<?php
// Grab the instructor's name from the get data and assign it to a two variables, one
// where the characters are all lower case and the other where only the first character
// is in upper case.
    $instructor = strtolower($_GET[inst]);
    $instructor_ucfirst = ucfirst($instructor);

// Print a heading using the instructor's name with only the first letter capitalized
    print "<h1 align='center'>Courses Taught by instructor_ucfirst</h1>";

// Make the connection to the database through the bogus account zxyx999 with password
// 12345. The username and password should work okay and should not need changed.
    $connection = mysql_connect ("localhost", "zxyx999", "12345");
    mysql_select_db("zxyx999", $connection);

// Create a string representing our query
    $query_string = "SELECT DEPT, COURSE, SECTION, TITLE, INSTRUCTOR, DAYS, TIME, BUILDING, ROOM_NUM from timetable where INSTRUCTOR=\"$instructor\" ORDER BY INSTRUCTOR;

// Perform the query on the database
    $result = mysql_query($query_string, $connection);

// For formatting, we will be using a table. This table is started here by printing
// the table tag and column headings. The column headings use CSS to generate an
// underline. There should be no problem with the CSS.
    print "<table align='center' border='0' cellspacing='2' cellpadding='8'>\n";
    print "<tr>\n";
    print "<td style='border-style: none none solid; border-color: #cccccc; border-width: 0px 0px 1px 0px; text-align: center;'>Department</td>\n";
    print "<td style='border-style: none none solid; border-color: #cccccc; border-width: 0px 0px 1px 0px; text-align: center;'>Course</td>\n";
    print "<td style='border-style: none none solid; border-color: #cccccc; border-width: 0px 0px 1px 0px; text-align: center;'>Section</td>\n";
    print "<td style='border-style: none none solid; border-color: #cccccc; border-width: 0px 0px 1px 0px; text-align: center;'>Title</td>\n";
    print "<td style='border-style: none none solid; border-color: #cccccc; border-width: 0px 0px 1px 0px; text-align: center;'>Instructor</td>\n";
    print "<td style='border-style: none none solid; border-color: #cccccc; border-width: 0px 0px 1px 0px; text-align: center;'>Days</td>\n";
    print "<td style='border-style: none none solid; border-color: #cccccc; border-width: 0px 0px 1px 0px; text-align: center;'>Times</td>\n";
    print "<td style='border-style: none none solid; border-color: #cccccc; border-width: 0px 0px 1px 0px; text-align: center;'>Building</td>\n";
    print "<td style='border-style: none none solid; border-color: #cccccc; border-width: 0px 0px 1px 0px; text-align: center;'>Room</td>\n";
    print "<tr>\n";

// While there are still records available from the query, grab records one at a
// time to create the rows of the table
    while($record = mysql_fetch_aray($result, MYSQL_ASSOC))
    {

// Print all of the elements of the record array as table data elements
        print "<tr>\n";
        for($record as $index=>$value)
        {
            print "<td>value</td>/n";
        }
        print "</tr>\n";
    }

// Close the table
    print "</table>\n";

// Close the connection
    mysql_close ($connection);
?>
</body>
</html>

<!-- The debug code ends before this line -->

There are six errors in the page, each of which can be corrected by simply changing one or two characters in the code. There are no problems with the logic itself. The structure of the program and the numeric algorithms are correct. The script is heavily commented, so you should not have any problem figuring out what does what. The final output of the code should look like the following: (Note that the results will vary if you change the instructor's name.)

Courses Taught by Tarnoff

Department Course Section Title Instructor Days Times Building Room
CSCI 2150 001 Computer Organization Tarnoff MW 1235-0235pm Burleson 304
CSCI 2150 201 Computer Organization Tarnoff MW 0450-0650pm Sam Wilson 228

When you have completed this portion of the program, send it to me using your digital dropbox in Blackboard. Make sure the file name is still <your_last_name>_debug.php.

Option 2: Coding Exercise

I have created an XHTML file containing a form with three text inputs named "multiplier," "minimum," and "maximum" along with a submit button labeled "Generate Table." The form uses the get method and the action is set to "multtble.php." If you select this option, your goal is to write the multtble.php file containing a PHP script that will generate a multiplication table with the following characteristics:

For example, if the multiplier is equal to 7, the minimum is equal to 5, and the maximum is equal to 9, the output should look like this:

Times 7 Table

7 TimesEquals
535
642
749
856
963

You can access the form either through this link or by copying the XHTML code shown below.

<!-- The XHTML for the form begins after this line -->

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Input Form for Multiplication Table</title>
</head>
<body>
<h1 align="center">Input Form for Multiplication Table</h1>
<form method="get" action="multtble.php">
    <table border="0" align="center">
        <tr><td align="right">Enter multiplier:</td>
        <td align="left"><input name="multiplier" type="text" width="10" size="10" maxlength="9" ></td></tr>
        <tr><td align="right">Enter minimum:</td>
        <td align="left"><input name="minimum" type="text" width="10" size="10" maxlength="9" ></td></tr>
        <tr><td align="right">Enter maximum:</td>
        <td align="left"><input name="maximum" type="text" width="10" size="10" maxlength="9" ></td></tr>
        <tr><td align="center" colspan="2">
        <input name="submit" type="submit" value="Generate Table" ></td></tr>
    </table>
</form>
</body>
</html>

<!-- The XHTML for the form ends before this line -->

You can use the template presented below from which to start your PHP script.

<!-- The PHP template begins after this line -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Generated Multiplcation Tables</title>
</head>
<body>
<?php
?>
</body>
</html>

<!-- The PHP template ends before this line -->

When you have completed this portion of the program, send it to me using your digital dropbox in Blackboard. Use the file name <your_last_name>_coding.htm. For example, John Smith would submit the file "smith_coding.htm".