SI 540: Fall 1999

HW 8 Solutions

due November 22

Last modified 11/23/99 --PR
Class home page

You can do this assignment in pairs, but not in groups larger than two.

Readings

Chapters 12, 18, 19. Note that chapter 12 covers a high-level, abstract view and 18 and 19 provide more details. If the abstract view seems confusing, you might try reading chapters 18 and 19 first, or even 19, then 18, then 12.

Text exericses

(2 points) E9.2

a. Internal development

Risks for the "buyer"

Risks for the "seller"

b. Outsourced development with fixed-price contract

Risks for the "buyer"

Risks for the "seller"

c. Outsourced development with time-and-materials contract

Risks for the "buyer"

Risks for the "seller"

d. Purchase software product

Risks for the "buyer"

Risks for the "seller" (if produce software before lining up buyers)

(2 points) E10.7, but you only need to give one example of each rather than 2.

Correct answers to these questions have the following characteristics:

--there's a software object

--there's a physical of informational entity

--the correct relation exists between the two (representation, model, or proxy)

An object that contains the academic calendar of the University and can answer questions about it (e.g., how many Monday sessions are there?)

An object that gets readings from a sensor (e.g., a thermometer) and can change the state of an effector (e.g., turning the heating system on or off).

An object that does transaction processing (requests for adding or removing funds) for a bank account.

An object that predicts how long it will take a room to heat up or cool down, based on the current temperature outside.

A combination of the thermometer/furnace interface object and the object that models room heating and cooling could be combined to make a "smart" thermostat.

PHP exercise

Solutions are in a zipped file. There are also links below to execute the scripts.

The SI web server is configured to do something special whenever there is a request for a file that ends in .php3. Instead of just sending the file's contents to the web browser, it treats the file's contents as a script. The script is passed to a PHP interpreter which executes the script. In the process of execution, it generates HTML, which is then sent to the web browser.

Your task in this exercise is to make a working PHP script that displays the following information about each course:
courseid, coursenumber, coursename. Then you'll make another script that generates XML, and modify an XSL stylesheet.

We've broken it down into a few steps for you. You should download the zipped file that contains all of the code below already saved as files. Save them in your HTML directory or wherever you save files in order to have them served up by the SI Web server.

Make a working PHP script.

First, save the script nodb.php3 (click here to execute) in your HTML directory or wherever you save files in order to have them served up by the SI Web server. Then, view the corresponding URL in a web browser. In the browser, view the HTML source. Notice that the "for" loop  expanded to generate an HTML table with several rows. If not, then your script was not processed through a PHP interpreter. Note that your web server may not have a PHP interpreter installed-- use the SI web server by putting the file in your HTML directory on the SI server.

<html>
 <head>
 <title>PHP Exercise</title>
 </head>
 <body>
  <?
    echo "<b>Your Query has returned 7 hits.</b>";
    echo "<p><table border=1>";
    for ($i=0;$i<7;$i++)
    {
      echo "<tr><td>";
      echo "column 1 row $i ", "</td><td>";
      echo "column 2 row $i", "</td></tr>";
    }
    echo "</table></p>";
  ?>
</body>
</html>

Step 2: Write a script that connects to your database

Now that you're sure you have a PHP script working, let's try one where you get results via an SQL query. Copy the following script (db.php3; click here to execute) into a file in your web space. Then edit it, by replacing the part that specifies the database "presnick" with your uniqname. Also, replace the HTML output that says it's presnick's database. Try running it to make sure it works.

<html>
<head>
<title>PHP Exercise</title>
</head>

<body>
<h1>Class information of 1997 from presnick's database</h1>

<?
	mysql_connect("ebola.si.umich.edu:3306","webuser","");
	$result = mysql("presnick", "SELECT coursenumber,coursename FROM course,class WHERE course.courseid=class.xrefcourseid AND class.yearoffered='1997' ORDER BY coursenumber");
	$num = mysql_numrows($result);
	
	echo "<b>Your Query has returned $num hits.</b>";
	echo "<p><table border=1>";
	for ($i=0;$i<$num;$i++)
	{
		echo "<tr><td>";
		echo mysql_result($result,$i,"coursenumber")."</td><td>";
		echo mysql_result($result,$i,"coursename")."</td></tr>";
	}

	mysql_close();
	echo "</table></p>"; 
?>

</body>
</html>

(2 points) Step 3: Edit the script to display the course id, too.

Now you need to edit the script to make it display the course id in addition to the two other fields.
Hint1: you'll need to adjust the SQL query to select the courseids as well as the other fields.
Hint2: try running your adjusted SQL query in the SSH window, to make sure you got it right.
Hint3: you'll need to add another "echo" command. 

In your HW, write include the URL of your page so that Junho can take a look at your page.

Source code for solutions is in a zipped file.  Check out dbsoln.php3.

Step 4: Generate XML.

Next, let's generate XML instead of HTML. Save the script resultsxml.phpxml into your web space (click here to run it).   Then edit it, by replacing the part that specifies the database "presnick" with your uniqname. You'll also need to save the stylesheet results.xsl into the same directory where you put the script. Try running it to make sure it works.

Note that you need to use the strange extension .phpxml for your script. This is to recover from an apparent bug in Microsoft's browser. Even though the script generates a header informing the browser that the content-type is text/xml, IE5, at least on my machine, did not run the preprocessor when the original url ended in .php. Junho has configured the SI Web server so that whenever it sees the .phpxml file extension, it runs the php interpreter, just as with .php3 file extensions. IE5 properly sends the results through its preprocessor when the original file extension is .phpxml, but when it is .php3.

<?
	header( "Content-type: text/xml" );
	echo "<?xml version=\"1.0\"?>";
	echo "<?xml-stylesheet type=\"text/xsl\" href=\"results.xsl\"?>";
	echo "<result>";
	mysql_connect("ebola.si.umich.edu:3306","webuser","");
	$result = mysql("presnick", "SELECT coursenumber,coursename FROM course,class WHERE course.courseid=class.xrefcourseid AND class.yearoffered='1997' ORDER BY coursenumber");
	$num = mysql_numrows($result);
	for ($i=0;$i<$num;$i++)
	{
		echo "<row>";
		echo "<NUM> ". mysql_result($result,$i,"coursenumber")." </NUM>";
		echo "<NAME> ". mysql_result($result,$i,"coursename")." </NAME>";
		echo "</row>";
	}
	mysql_close();
	echo "</result>";
?>

End of script; beginning of stylesheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  <xsl:template match="/">
    <HTML>
      <body>
	<h1>Class information of 1997 from presnick's database</h1>
	<table border="1">
        <xsl:for-each select="result/row">
            <TR>
              <TD><xsl:value-of select="NUM"/></TD>
              <TD><xsl:value-of select="NAME"/></TD>
            </TR>
        </xsl:for-each>
        </table>
      </body>
    </HTML>
  </xsl:template>
</xsl:stylesheet>

End of stylesheet; what follows is a sample of XML that the script might generate. You can find it in the file simple.xml.

<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="results.xsl"?>
<result>
<row><NUM> 501 </NUM><NAME> Use of Information </NAME></row>
<row><NUM> 502 </NUM><NAME> Choice and Learning </NAME></row>
<row><NUM> 511 </NUM><NAME> Technology in Design: Methods and Means </NAME></row>
</result>

(2 points) Step 5: Edit the script and the Stylesheet

Now you need to edit the script to make it display the course id in addition to the two other fields.

Hint1: use the same SQL query from step 3.
Hint2: you'll need to add another "echo" command, and invent another XML tag to use for the course id.
Hint3: you'll need to add another line to the XSL style sheet to say how to handle your new tag.

Debugging hint: if you don't get the output you want, is it because the script is generating bad XML, or because there's an error in you style sheet? Here's how to find out. Remove the line from the script that specifies the stylesheet:
echo "<?xml-stylesheet type=\"text/xsl\" href=\"results.xsl\"?>";
Try accessing the URL, and you'll see the raw XML that your script generated. If it looks good, the problem is probably with your stylesheet. Put that line back in your script, access the URL again, and then view source on the output to see what HTML was generated.

Source code for solutions is in a zipped file. See xmlsoln.phpxml.

Extra Credit

Haven't had enough fun yet? Here are some ideas for more things to try out for extra credit.

   $headers = getallheaders();
   $browserheader = "User-Agent";
   echo "$headers[$browserheader]";

Source code for solutions is in a zipped file. I'll add student solutions to the extra credit questions to the zip file, as Junho identifies them for me.

Explanation exercise (2)

Explain the architecture of database-driven websites. That is, explain what the components are (browser, web server, database server), what functions each performs, and how they interact. Be sure to explain what the role of the PHP script is.

For this, you basically want to cover the diagram from the lecture notes.