Test 3/Spring '07 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 "zxyx999" 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);

// 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 mylibrary, 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:

Desired output from modify portion of test.

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.php. For example, John Smith would submit the file "smith_modify.php".

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 a single table of movies created with the following script:

DROP TABLE movie_inventory;
CREATE TABLE movie_inventory(NAME VARCHAR(50), PRICE DECIMAL(7,2), TYPE ENUM('DVD', 'Blu-ray'), PUB_YR YEAR(4));
INSERT INTO movie_inventory VALUES ('Happy Feet', 15.99, 'DVD', 2006);
INSERT INTO movie_inventory VALUES ('Charlottes Web', 16.99, 'DVD', 2006);
INSERT INTO movie_inventory VALUES ('Peter Pan', 16.99, 'DVD', 1953);
INSERT INTO movie_inventory VALUES ('Planet Earth', 69.95, 'Blu-ray', 2007);
INSERT INTO movie_inventory VALUES ('Casino Royale', 26.95, 'Blu-ray', 1967);
INSERT INTO movie_inventory VALUES ('Cars', 16.99, 'DVD', 2006);
INSERT INTO movie_inventory VALUES ('Pirates of the Caribbean - Dead Mans Chest', 24.99, 'DVD', 2006);
INSERT INTO movie_inventory VALUES ('Da Vinci Code, The', 19.99, 'DVD', 2006);
INSERT INTO movie_inventory VALUES ('Flushed Away', 15.99, 'DVD', 2006);
INSERT INTO movie_inventory VALUES ('Open Season', 16.99, 'DVD', 2006);
INSERT INTO movie_inventory VALUES ('Guardian, The', 16.99, 'DVD', 2006);
INSERT INTO movie_inventory VALUES ('Departed, The', 20.49, 'DVD', 2006);
INSERT INTO movie_inventory VALUES ('Blue Planet, The', 34.49, 'DVD', 2002);
INSERT INTO movie_inventory VALUES ('Black Hawk Down', 19.95, 'Blu-ray', 2001);
INSERT INTO movie_inventory VALUES ('Departed, The', 23.95, 'Blu-ray', 2006);

The data from the form is assigned to the form input identified as "find". It is simply a string of characters to search on the movie name (field NAME). 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 "?find=the" or "?find=blue" (without the quotation marks) to the end of the URL, you will send the get data of a search string in the form variable find. For example, the URL for a search on this database for the string "de" in a movie name would look like http://einstein.etsu.edu/~zxyx999/smith_debug.php?find=de

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.php".

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

<?php
    print "<?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>Movie Database Access</title>
</head>
<body>

<?php

// First, grab the search string from the $_GET array
    $search_str = $GET['find'];

// Print a heading including the search string
    print "<h1 align='center'>Results of a search on '{$search_str}'</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);

// Perform the query on the database
    $result = mysql_querey("SELECT * FROM movie_inventory where NAME like '%{$search_str}%' order by NAME", $connection);

// Initialize a counter to count through all of the records
    $count=0;

// Print the starting tag for the table
    print "<table border='2' cellpadding='3' align='center'>";
    while($record = mysql_fetch_array($result, MYSQL_ASSOC))
    {

// If we are still on the first record, print the table headings as the field names
        if($count = 0)
        {
            print "<tr>";
            foreach($record as $index=>$value)
                print "<td align='center'>{$index}</td>\n";
            print "</tr>";
        }

// Now print the fields for each record
        $count++;
        print "<tr>";
        foreach($record as $index->$value)
                print "<td>{value}</td>\n";
        print "</tr>";
    }

// Print the ending tag for the table
    print "</table>";

// Close the database connection
    mysql_close ($connection);
?>

</body>
</html>

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

There are five 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 search string.)

Results of a search on 'de'

NAME PRICE TYPE PUB_YR
Da Vinci Code, The 19.99 DVD 2006
Departed, The 20.49 DVD 2006
Departed, The 23.95 Blu-ray 2006
Pirates of the Caribbean - Dead Mans Chest 24.99 DVD 2006

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 a single large textarea named "toprocess." and a submit button labeled "Process Text." The form uses the post method and the action is set to "coding.php." (You will need to change the method so it executes your PHP file.) If you select this option, your goal is to write the coding.php file containing a PHP script that will perform the following functions on the text contained in "toprocess":

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>Analyzing a String</title>
</head>
<body>
<h1 align="center">Analyzing a String</h1>
<!-- NOTE: BE SURE TO CHANGE THE ACTION TO THE NAME OF YOUR OWN PHP FILE! -->
<form method="post" action="coding.php">
    <div align="center">
        <textarea cols="50" rows="8" name="toprocess">
Enter your text to be analyzed here.
        </textarea>
        <br />
        <input name="submit" type="submit" value="Process Text" ></td></tr>
    </div>
</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>String Processing Application</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.php. For example, John Smith would submit the file "smith_coding.php".