Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/sql-intro/sql-intro3/
create-car.php
createtable.php
insertgarage-owner.php
sql-intro3-index.php
sql-intro3a.php
sql-intro3b.php
sql-intro3c.php
sql-intro3d.php
sql-intro3a.php
98 lines UTF-8 Windows (CRLF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
th{
background-color:rgb(56, 56, 56);
color: white;
}
h2{
margin:0;
}
table{
width:500px;
border-radius:5px;
border: solid 3px black;
}
td{
width:150px;
}
th{
border-radius:2px;
}
</style>
</head>
<body>
<?php
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current(): string {
return "<td>" . parent::current(). "</td>";
}
function beginChildren(): void {
echo "<tr>";
}
function endChildren(): void {
echo "</tr>" . "\n";
}
}
include ('../../../dbconnection.php');
try {
echo "<table>";
echo "<h2>Garage</h2>";
echo "<tr><th>Id</th><th>Garage Namn</th></tr>";
$stmt = $dbconn->prepare("SELECT garageid, name FROM garage");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
echo "<table>";
echo "<br> <br>";
echo "<h2>Ägare</h2>";
echo "<tr><th>Id</th><th>Ägarens Namn</th></tr>";
$stmt = $dbconn->prepare("SELECT ownerid, name FROM owner");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
echo "<table>";
echo "<br> <br>";
echo "<h2>Bilar</h2>";
echo "<tr><th>Id</th><th>Regplåt</th><th>Färg</th><th>Garage Id</th><th>Ägar Id</th></tr>";
$stmt = $dbconn->prepare("SELECT carid, regnr, color, garage, owner FROM car");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error:" . $e->getMessage();
}
$dbconn = null;
?>
</body>
</html>