University of Washington, CSE 190 M, Spring 2009
Lab 10: Final Exam Review ANSWER KEY
1. JavaScript/DOM/Prototype
window.onload = function() {
var tables = $$("table");
for (var i = 0; i < tables.length; i++){
var rows = tables[i].getElementsByTagName("tr");
for (var j = 0; j < rows.length; j += 2) {
rows[j].addClassName("zebrastripe");
}
}
};
2. Ajax/XML
window.onload = function() {
new Ajax.Request("rect.php", {
method: "GET",
onSuccess: createRects
});
};
function createRects(ajax) {
var rects = ajax.responseXML.getElementsByTagName("rectangle");
for (var i = 0; i < rects.length; i++) {
var div = document.createElement("div");
div.style.position = "absolute";
div.style.border = "2px solid black";
div.style.width = rects[i].getAttribute("width") + "px";
div.style.height = rects[i].getAttribute("height") + "px";
div.style.left = rects[i].getAttribute("x") + "px";
div.style.top = rects[i].getAttribute("y") + "px";
div.style.backgroundColor = "#" + rects[i].getAttribute("color");
$("rectanglearea").appendChild(div);
}
}
3. PHP
<?php
header("Content-Type: text/plain");
$search_name = $_REQUEST["name"];
$found = FALSE;
$text = file_get_contents("employees.txt");
$lines = explode("\n", $text);
foreach ($lines as $line) {
$tokens = preg_split("/\t+/", $line);
if ($tokens[0] == $search_name) {
$found = TRUE;
$title = preg_replace("/[^a-zA-Z]+/", "", $tokens[2]);
print "http://www.awesomeco.com/$title/{$tokens[1]}";
}
}
if (!$found) {
print "http://www.awesomeco.com/";
}
?>
4. SQL
SELECT a1.first_name, a2.first_name, a2.last_name, m.name
FROM movies m
JOIN roles r1 ON r1.movie_id = m.id
JOIN actors a1 ON a1.id = r1.actor_id
JOIN roles r2 ON m.id = r2.movie_id
JOIN actors a2 ON a2.id = r2.actor_id
WHERE a1.last_name = a2.last_name
AND a1.first_name < a2.first_name
ORDER BY a2.last_name;
5. PHP+HTML+SQL
function display_last_names() {
# connect to database
$db = mysql_connect("localhost", "user", "password");
mysql_select_db("imdb_small");
$query = "SELECT a1.first_name AS first1, a2.first_name AS first2, a2.last_name AS last, m.name AS movie
FROM movies m
JOIN roles r1 ON r1.movie_id = m.id
JOIN actors a1 ON a1.id = r1.actor_id
JOIN roles r2 ON m.id = r2.movie_id
JOIN actors a2 ON a2.id = r2.actor_id
WHERE a1.last_name = a2.last_name
AND a1.first_name < a2.first_name
ORDER BY last;";
$results = mysql_query($query);
?>
<ol>
<?php
$second = FALSE;
while ($row = mysql_fetch_array($results)) {
if (!$second && preg_match("/^[K-Z]/", $row["last"])) {
# end first list and start second
$second = TRUE;
?>
</ol>
<ol>
<?php
}
?>
<li><?= $row["last"] ?>, <?= $row["first1"] ?> / <?= $row["first2"] ?>: <?= $row["movie"] ?></li>
<?php
}
?>
</ol>
<?php
}