<!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">

	<head>
	
		<title>Los Angeles Stadt der Engel</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel='stylesheet' media='all' type="text/css" href="stylesheet.css" />
		<style type="text/css"></style>

	</head>
	
	<body>

		<?php 
			require_once("db_connection.php");
			require_once("ml.txt");

			echo "<div class='data' style='overflow: scroll;'>\n";
			echo "<B><p class='menue'>Vergleich der Linien</p></B>\n";
			
			//Table lines ordered by distance
			echo "<B><p class='submenue'>Linien geordnet nach durchschnitlicher Streckenlänge zwischen zwei Stationen</p></B>\n";
			echo "<table>\n";
			echo "\t<tr><th>Liniennummer</th><th>Streckenlänge</th></tr>\n";
			$query1 = "SELECT id, MAX(distance) AS maxdistance FROM stopsonroutes, routes WHERE routes.id = stopsonroutes.rid GROUP BY id ORDER BY MAX(distance)";
			$sth1 = mysql_query($query1) OR die("Fehler: ". mysql_error());
			$count1 = 0;
			$dist1 = 0;
			while ($object = mysql_fetch_object($sth1)) {
				//counts the number of rows of results (number of routes)
				$count1=$count1+1;
				//counts all maxdistances of all routes
				$dist1=$dist1+$object->maxdistance;
				echo "\t<tr><td>$object->id</td><td>$object->maxdistance</td></tr>\n";
			}
			echo "</table><br />\n";
			$avg1 = round($dist1/$count1);
			echo "Die Streckenlänge zwischen zwei Stationen beträgt durchschnittlich $avg1 m.<br /><br />\n";
			mysql_free_result($sth1);
			
			//Table lines ordered by time
			echo "<B><p class='submenue'>Linien geordnet nach der Fahrzeit</p></B>\n";
			echo "<table>\n";
			echo "\t<tr><th>Liniennummer</th><th>Fahrzeit</th></tr>\n";
			$query2 = "SELECT id, MAX(duration) AS maxduration FROM stopsonroutes, routes WHERE routes.id = stopsonroutes.rid GROUP BY id ORDER BY MAX(duration)";
			$sth2 = mysql_query($query2) OR die("Fehler: ". mysql_error());
			$count2 = 0;
			$dist2 = 0;
			while ($object = mysql_fetch_object($sth2)) {
				$count2 = $count2+1;
				$dist2 = $dist2+$object->maxduration;
				echo "\t<tr><td>$object->id</td><td>$object->maxduration</td></tr>\n";
			}
			echo "</table><br />\n";
			$avg2=round($dist2/$count2);
			echo "<br /><br />\n";
			//Die Fahrzeit der Linien beträgt durchschnittlich $avg2 min.
			mysql_free_result($sth2);
			
			//table ordered by number of stops
			echo "<B><p class='submenue'>Linien geordnet nach der Anzahl der Haltestellen</p></B>\n";
			echo "<table>\n";
			echo "\t<tr><th>Liniennummer</th><th>Haltestellenanzahl</th></tr>\n";
			$query3 = "SELECT count(stopsonroutes.sid) AS count, routes.id AS line
						FROM stopsonroutes, routes
						WHERE stopsonroutes.rid=routes.id
						GROUP BY stopsonroutes.rid
						ORDER BY count(stopsonroutes.sid);";
			$sth3 = mysql_query($query3) OR die("Fehler: ". mysql_error());
			$count3=0;
			$dist3=0;
			while ($object = mysql_fetch_object($sth3)) {
				$count3=$count3+1;
				$dist3=$dist3+$object->count;
				echo "\t<tr><td>$object->line</td><td>$object->count</td></tr>\n";
			}
			echo "</table><br />\n";
			$avg3=round($dist3/$count3);
			echo "Die durchschnittliche Haltestellenanzahl der Linien beträgt $avg3.\n";
			mysql_free_result($sth3);
			
			echo "</div>\n";
			mysql_close($db);
		?>
	
	</body>
</html>