Källkod
Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.
webbserverprogrammering/exercises/mysql/exercise_3/exercise_3_e/
7 filer
back_button.php
dbconnection.php
index.php
insert_values.php
print_table.php
reset.php
reset_garages_owners.php
dbconnection.php
index.php
insert_values.php
print_table.php
reset.php
reset_garages_owners.php
print_table.php
146 lines ASCII Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Show cars</title>
<meta charset="utf-8">
<style type="text/css">
table.outputData {
border-collapse: collapse;
margin: 2em 3em;
display: inline-block;
vertical-align: top;
}
.outputData caption {
margin-bottom: 1em;
}
.outputData th {
font-weight: bold;
}
.outputData td, .outputData th {
padding: 8px;
border: 1px solid #686868;
}
</style>
</head>
<body>
<?php
include('back_button.php');
include ('dbconnection.php');
try {
$sql = "SELECT * FROM garages";
$stmt = $dbconn->prepare($sql);
$stmt->execute();
$output = "
<table class='outputData'>
<caption>
<strong>Garages</strong>
</caption>
<tr>
<th>Index</th>
<th>Name</th>
</tr>
";
$garages = $stmt->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP | PDO::FETCH_UNIQUE);
foreach ($garages as $id => $garageInfo) {
$output .= "
<tr>
<td>".htmlentities($id)."</td>
<td>".htmlentities($garageInfo['name'])."</td>
</tr>";
}
$output .= "</table>";
echo $output;
}
catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
try {
$sql = "SELECT * FROM owners ORDER BY name ASC";
$stmt = $dbconn->prepare($sql);
$stmt->execute();
$output = "
<table class='outputData'>
<caption>
<strong>Owners</strong>
</caption>
<tr>
<th>Index</th>
<th>Name</th>
</tr>
";
$owners = $stmt->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP | PDO::FETCH_UNIQUE);
foreach ($owners as $id => $ownerInfo) {
$output .= "
<tr>
<td>".htmlentities($id)."</td>
<td>".htmlentities($ownerInfo['name'])."</td>
</tr>";
}
$output .= "</table>";
echo $output;
}
catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
try {
$sql = "SELECT
cars.carId,
cars.registrationNmbr,
cars.color,
garages.name AS garage,
owners.name AS owner
FROM cars
INNER JOIN garages ON cars.garage=garages.garageId
INNER JOIN owners ON cars.owner=owners.ownerId
ORDER BY garage ASC, owner ASC"; // "You can't use a column alias in ON or WHERE" but in ORDER BY works fine! :D
$stmt = $dbconn->prepare($sql);
$stmt->execute();
$output = "
<table class='outputData'>
<caption>
<strong>Cars</strong>
</caption>
<tr>
<th>Index</th>
<th>Registration</th>
<th>Color</th>
<th>Garage</th>
<th>Owner</th>
</tr>
";
while ($response = $stmt->fetch(PDO::FETCH_ASSOC)) {
$output .= "<tr>
<td>".htmlentities($response['carId'])."</td>
<td>".htmlentities($response['registrationNmbr'])."</td>
<td>".htmlentities($response['color'])."</td>
<td>".htmlentities($response['garage'])."</td>
<td>".htmlentities($response['owner'])."</td>
</tr>";
}
$output .= "</table>";
echo $output;
}
catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$dbconn = null;
?>
</body>
</html>