Show sourcecode
The following files exists in this folder. Click to view.
admin.php
fetch_car_things.php
fetch_kompisar.php
fetch_users.php
mysql1.php
mysql2.php
mysql3.php
mysql3_satt_att_sortera.php
mysql3car.php
mysql3garage.php
mysql3owner.php
welcome.php
mysql3_satt_att_sortera.php
88 lines UTF-8 Windows (CRLF)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
<?php
include('../../incl/dbconnection.php');
/**
* @var PDO $dbconn
*/
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Olika sätt att joina på</title>
</head>
<body>
<h1>Olika sätt att sortera data på</h1>
<form action="" method="get">
<select name="sort_type">
<option value="sort_name">Sortera efter personnamn</option>
<option value="sort_garage">Sortera efter garage</option>
<option value="sort_garage_name">Sortera efter garage och namn inom garage</option>
</select>
<input type="submit" value="Hämta data">
</form>
<table>
<thead>
<th>id</th>
<th>Reg.nummer</th>
<th>Färg</th>
<th>Garage</th>
<th>Ägare</th>
</thead>
<tbody>
<?php
$sort_type = isset($_GET['sort_type']) ? $_GET['sort_type'] : null;
// Endast kör query om en sorteringstyp är vald
if ($sort_type) {
$sql = "SELECT cars.car_id, cars.licence_plate, cars.color, garage.name AS garage_name, owners.name AS owner_name
FROM cars
JOIN owners ON cars.owner = owners.owner_id
JOIN garage ON cars.garage = garage.garage_id";
$orderBy = "";
switch ($sort_type) {
// Sortera efter ägarnamn
case "sort_name":
$orderBy = " ORDER BY owners.name";
break;
// Sortera efter garagenamn
case "sort_garage":
$orderBy = " ORDER BY garage.name";
break;
// Sortera efter garage och sedan ägarnamn
case "sort_garage_name":
$orderBy = " ORDER BY garage.name, owners.name";
break;
default:
// Ogiltig sorteringstyp, kör ingen query
$sql = "";
echo "<tr><td colspan='5'>Välj en giltig sorteringstyp.</td></tr>";
}
if (!empty($orderBy)) {
$sql .= $orderBy;
}
if (!empty($sql)) {
$stmt = $dbconn->query($sql);
foreach ($stmt as $row) {
echo "<tr>" .
"<td>" . htmlentities($row['car_id']) . "</td>" .
"<td>" . htmlentities($row['licence_plate']) . "</td>" .
"<td>" . htmlentities($row['color']) . "</td>" .
"<td>" . htmlentities($row['garage_name']) . "</td>" .
"<td>" . htmlentities($row['owner_name']) . "</td>" .
"</tr>";
}
}
}
?>
</tbody>
</table>
</body>
</html>