<?php
// ab4.php - An welcher Haltestelle h&auml;lt mehr als eine Linie und welche Linien halten dort? 

include 'header.php';


// ############ START QUERY ############################################################
$abfrage = "
SELECT stations.name, COUNT(routes.id) AS 'Anzahl', GROUP_CONCAT(DISTINCT routes.name ORDER BY routes.name SEPARATOR ', ') AS 'Linien'  
FROM stations, routes, routes_stations
WHERE routes.id = routes_stations.r_id
AND stations.id = routes_stations.s_id
GROUP BY stations.id
HAVING COUNT(routes.id) > 1
ORDER BY Anzahl DESC, stations.name ASC";
$ergebnis = mysqli_query($db, $abfrage) or die ("Anfrage fehlgeschlagen: " . mysqli_error($db));



echo '</head>';
// ############ START BODY ############################################################
echo '<body>';

include 'menu.php';


// ############ START CONTENT ############################################################
echo '
   <div id="content">
   <a href="abfragen.htm" class="menulink">zur&uuml;ck zu den Abfragen</a>
';   

echo '<h1>An welcher Haltestelle h&auml;lt mehr als eine Linie und welche Linien halten dort?</h1>';

echo "
<table border=\"0\">
<tr>
  <td>
    <b>Haltestelle</b>
  </td>
  <td>
    <b>Anzahl der Linien&nbsp;&nbsp;</b>
  </td>
  <td>
	<b>Linien</b>
  </td>
</tr>
";

while ($line = mysqli_fetch_array($ergebnis, MYSQLI_ASSOC)) {
   echo "\t<tr>\n";
   foreach ($line as $col_value) {
      echo "\t\t<td>$col_value&nbsp;&nbsp;</td>\n";
   }
   echo "\t</tr>\n";
}
echo "</table>\n";


include 'footer.php';
?>
