This repository has been archived on 2026-06-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
sgd/ysqld/ysql08d/HA/test4.php
T

68 lines
3.0 KiB
PHP

<!DOCTYPE HTML>
<html lang="de">
<head>
<meta charset="utf-8">
<title>Länderdaten nach Regionen</title>
<?php
include_once("db.inc.php");
?>
</head>
<body>
<form method="post">
<?php
// Select-Auswahl für Regionen
$sql = "SELECT region_id, name FROM regions ORDER BY name";
$stmt = $pdo->prepare($sql);
$stmt->execute();
echo "<select name='region' id='region'>";
echo "<option value=''>Region wählen</option>";
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $region) {
$selected = (isset($_POST['region']) && $_POST['region'] == $region['region_id']) ? 'selected' : '';
echo "<option value='" . htmlspecialchars($region['region_id']) . "' $selected>" .
htmlspecialchars($region['name']) . "</option>";
}
echo "</select>";
// Ausgabe der Daten
if(isset($_POST["region"]) && !empty($_POST["region"])){
try {
$auswahl = "SELECT countries.name AS Land, countries.area AS 'Fläche', AVG(country_stats.gdp) AS 'AVG BIP', AVG(country_stats.population) AS 'AVG Bevölkerung', continents.name AS Kontinent from regions
JOIN continents on continents.continent_id = regions.continent_id
JOIN countries on regions.region_id = countries.region_id
JOIN country_stats on countries.country_id = country_stats.country_id
WHERE regions.region_id = :region_id GROUP BY countries.country_id, countries.name, countries.area, continents.name";
$stmt = $pdo->prepare($auswahl);
$stmt->bindParam(':region_id', $_POST['region'], PDO::PARAM_INT);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(count($results) > 0) {
echo "<h2>Länder in der ausgewählten Region:</h2>";
echo "<table border='1'>";
echo "<tr><th>Land</th><th>Fläche</th><th>AVG BIP</th><th>AVG Bevölkerung</th><th>Kontinent</th></tr>";
foreach($results as $row) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row['Land']) . "</td>";
echo "<td>" . number_format($row['Fläche'], 0, ',', '.') . "</td>";
echo "<td>" . number_format($row['AVG BIP'], 2, ',', '.') . "</td>";
echo "<td>" . number_format($row['AVG Bevölkerung'], 0, ',', '.') . "</td>";
echo "<td>" . htmlspecialchars($row['Kontinent']) . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "<p>Keine Daten.</p>";
}
} catch(PDOException $e) {
echo "<p>Fehler bei der Datenbankabfrage: " . htmlspecialchars($e->getMessage()) . "</p>";
}
}
?>
<br>
<input type="submit" value="anzeigen">
</form>
</body>
</html>