University of Washington, CSE 154
Lab 10: Practice Final Exam Problems, ANSWER KEY

1. Ajax/XML

window.onload = function() {
    var ajax = new XMLHttpRequest();
    ajax.onload = createRects;
    ajax.open("GET", "rect.php", true);
    ajax.send();
};

function createRects() {
    var rects = this.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");
        document.getElementById("rectanglearea").appendChild(div);
    }
}

2. JSON



window.onload = function(){
	var ajax = new XMLHttpRequest();
	ajax.onload = load;
	ajax.open("GET", "data.php", true);
	ajax.send();
};

function load(){
	var json = JSON.parse(this.responseText).shapes;
	for(var i = 0; i < json.length; i++){
		var div = document.createElement("div");
		div.style.position = "absolute";
		div.style.top = json[i].y;
		div.style.left = json[i].x;
		div.style.backgroundColor = "#" + json[i].color;
		div.style.width = json[i].width;
		div.style.height = json[i].height;
		document.getElementById("rectangles").appendChild(div);
	}
}

			

3. Web Service


<?php
	$name = $_GET["name"];

	$department = $_GET["department"];

	$lines = file($name . ".txt", FILE_IGNORE_NEW_LINES);

	$final_result = array();

	$all_classes = array();

	foreach($lines as $line){
		$class_info = array();
		list($class, $number, $time) = explode(":", $line);
		if($class == $department){
			$class_info["name"] = $class . " " . $number;
			$class_info["quarter"] = $time;
			array_push($all_classes, $class_info);
		}
	}

	$final_result["classes"] = $all_classes;

	header("Content-type: application/json");

	print(json_encode($final_result));


?>


			

4. PHP

<?php
header("Content-Type: text/plain");
$search_name = $_GET["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/";
}
?>

5. 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;

6. PHP+HTML+SQL

function display_last_names() {
    # connect to database
    $db = new PDO("mysql:dbname=imdb", "username", "password");
    $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";
    $rows = $db->query($query);
    ?>

    <ol>

        <?php
        $second = FALSE;
        foreach ($rows as $row) {
            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
}