Tabellenkalender Blutdruckwerte
Daten einer CSV-Datei dynamisch in eine HTML-Tabelle einbinden. Die Daten können eingetragen, sortiert, gefiltert und wieder gelöscht werden.

- Daten in einer CSV-Datei
- Sortieren der Tabelle
- Filtern der Tabelle
- Datum, Uhrzeit und Ereignis eintragen
- Ereignisse löschen
- Passwortschutz
Quelltext: tabellenkalender.php Einblenden ❘ Kopieren ❘ Link ❘ Zeilen ❘
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/*
* Tabellenkalender
* https://werner-zenk.de
*/
$verzeichnis = "daten.csv";
$name = "user";
$passwort = "0000";
date_default_timezone_set("Europe/Berlin");
if (isset($_GET["aktualisieren"])) {
// Name und Passwort überprüfen
if (
$_POST["name"] == $name &&
$_POST["passwort"] == $passwort
) {
// Eintragen
if ($_POST["option"] == "eintragen") {
if (
strpos($_POST["datum"], "T") &&
!empty($_POST["ereignis"])
) {
$daten = file_get_contents($verzeichnis);
$datum = str_replace("T", " ", $_POST["datum"]);
$ereignis = strip_tags($_POST["ereignis"]);
$ereignis = str_replace(";", "", $ereignis);
$daten .= PHP_EOL . $datum . ';' . $ereignis;
file_put_contents($verzeichnis, $daten);
}
}
// Löschen
if ($_POST["option"] == "loeschen") {
$daten = file($verzeichnis, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$index = array_search($_POST["id"], $daten);
if ($index !== false) {
array_splice($daten, $index, 1);
file_put_contents($verzeichnis, implode(PHP_EOL, $daten));
}
}
}
}
// Auslesen
if (
isset($_GET["auslesen"]) ||
isset($_GET["aktualisieren"])
) {
$daten = file($verzeichnis, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$tabelle = '
<table id="foobar-table">
<thead>
<tr>
<th data-type="number" title="Nach Nummer sortieren">#</th>
<th data-type="number" title="Nach Datum sortieren">Datum</th>
<th data-type="number" title="Nach Uhrzeit sortieren">Uhr</th>
<th data-type="string" title="Nach Ereignis sortieren">Ereignis</th>
<td><input type="radio" name="id" value="0" checked="checked" title="Kein Ereignis auswählen"></td>
</tr>
</thead>
<tbody>';
foreach ($daten as $nr => $data) {
list($datum, $ereignis) = explode(";", $data);
sscanf($datum, "%4s-%2s-%2s %6s", $jahr, $monat, $tag, $uhr);
$tabelle .= '<tr><td>' . ($nr + 1) . '</td><td>' . $jahr . '-' . $monat . '-' . $tag . '</td><td>' . $uhr . '</td><td>' . $ereignis .
'</td><td><input type="radio" name="id" value="' . $datum . ';' . htmlspecialchars($ereignis) . '" title="Ereignis auswählen"></td></tr>';
}
$tabelle .= '</tbody>
</table>';
exit($tabelle);
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tabellenkalender</title>
<style>
body {
font-family: system-ui, -apple-system, "Segoe UI", "Roboto", "Ubuntu", "Cantarell", "Noto Sans", "Verdana", sans-serif;
}
table#foobar-table {
margin-left: 85px;
}
/* Tabellenkopf festsetzen */
table#foobar-table thead {
position: sticky;
inset-block-start: 0;
user-select: none;
}
/* Hintergrund Tabellenkopf */
table#foobar-table>thead>tr {
background-color: rgba(255, 174, 94, 0.8);
}
table#foobar-table>thead>tr:hover {
background-color: #FFAE5E !important;
cursor: pointer;
}
/* Die Spalten der Tabelle anpassen */
table#foobar-table,
table#foobar-table>thead>tr>th,
table#foobar-table>tbody>tr>td {
border: Solid 1px #888888;
padding: 5px 8px 5px 8px;
color: #000000;
}
/* Spalte in der Tabelle von der Sortierung ausschließen */
table#foobar-table>thead>tr>td {
border: Solid 1px #888888;
padding: 5px 8px 5px 8px;
color: #000000;
font-weight: bold;
text-align: center;
}
/* Den Hintergrund zeilenweise einfärben */
table#foobar-table>tbody>tr:nth-child(even) {
background-color: #FFFFFF;
}
table#foobar-table>tbody>tr:nth-child(odd) {
background-color: #FFD2A6;
}
/* Pfeil nach oben */
.upArrow::after {
content: "\2191";
color: #FFFFFF;
text-shadow: 1px 1px 0px #000000;
position: absolute;
}
/* Pfeil nach unten */
.dnArrow::after {
content: "\2193";
color: #FFFFFF;
text-shadow: 1px 1px 0px #000000;
position: absolute;
}
input[name="ereignis"],
input[ id="filter"] {
width: 12.5rem;
}
label[for] {
display: inline-block;
width: 80px;
}
input[type="radio"] {
accent-color: rgb(255, 130, 4);
width: 15px;
height: 15px;
}
summary {
cursor: Pointer;
user-select: None;
}
summary:hover,
label:hover {
color: rgb(255, 174, 94);
}
summary::marker {
color: rgb(255, 174, 94);
}
input[type="text"],
input[type="password"],
input[type="search"],
input[type="datetime-local"],
button[type="button"] {
font-family: system-ui, -apple-system, "Segoe UI", "Roboto", "Ubuntu", "Cantarell", "Noto Sans", "Verdana", sans-serif;
border: Solid 1px #cccccc !important;
padding: 5px;
}
button[type="button"]:hover {
background-color: rgb(255, 174, 94);
}
:focus-visible {
outline: Solid 1px rgb(255, 174, 94);
}
</style>
</head>
<body>
<form id="form" method="post">
<p>
<label for="filter">Filter:</label>
<input type="search" class="filter-table" id="filter" data-for="#foobar-table">
</p>
<p id="ausgabe"></p>
<details>
<summary>Eintragen oder löschen</summary>
<p>
<label for="datum">Datum:</label>
<input type="datetime-local" name="datum" id="datum" value="<?= date("Y-m-d\TH:i"); ?>">
</p>
<p>
<label for="ereignis">Ereignis:</label>
<input type="text" name="ereignis" id="ereignis">
</p>
<p>
<label><input type="radio" name="option" value="eintragen" checked="checked" required="required" title="Ereignis eintragen"> Eintragen</label>  
<label><input type="radio" name="option" value="loeschen" title="Ereignis löschen"> Löschen</label>
</p>
<p>
<label for="name">Name:</label>
<input type="text" name="name" id="name" autocomplete="username" required="required">
</p>
<p>
<label for="passwort">Passwort:</label>
<input type="password" name="passwort" id="passwort" autocomplete="current-password" required="required">
</p>
<p><button type="button" id="button">Absenden</button></p>
</details>
</form>
<script>
// Auslesen
fetch(`${document.URL}?auslesen`, {
method: "GET",
})
.then((antwort) => {
return antwort.text();
})
.then((antwort) => {
document.getElementById("ausgabe").innerHTML = antwort;
initialisieren();
});
// Aktualisieren
document.querySelector("#button").addEventListener("click", () => {
if (document.getElementById("form").reportValidity()) {
fetch(`${document.URL}?aktualisieren`, {
method: "POST",
body: new FormData(document.getElementById("form")),
})
.then((antwort) => {
return antwort.text();
})
.then((antwort) => {
document.getElementById("ausgabe").innerHTML = antwort;
document.getElementById("form").reset();
initialisieren();
});
}
});
function initialisieren() {
// HTML-Tabelle sortieren
const table = document.getElementById("foobar-table");
const headers = table.querySelectorAll("th");
const tableBody = table.querySelector("tbody");
const rows = tableBody.querySelectorAll("tr");
const directions = Array.from(headers).map(function(header) {
return "";
});
const transform = function(index, content) {
const type = headers[index].getAttribute("data-type");
switch (type) {
case "number":
return parseFloat(content);
case "string":
default:
return content;
}
};
const sortColumn = function(index, headers) {
const cls = ["upArrow", "dnArrow"];
headers.forEach((header) => {
document.getElementById(header.id).classList.remove(...cls);
});
const direction = directions[index] || "asc";
const multiplier = (direction === "asc") ? 1 : -1;
const cssSort = (direction === "asc") ? cls[0] : cls[1];
document.getElementById(index).classList.add(cssSort);
const newRows = Array.from(rows);
newRows.sort((rowA, rowB) => {
const cellA = encodeURIComponent(rowA.querySelectorAll("td")[index].textContent.toLowerCase());
const cellB = encodeURIComponent(rowB.querySelectorAll("td")[index].textContent.toLowerCase());
const a = transform(index, cellA);
const b = transform(index, cellB);
switch (true) {
case a > b:
return 1 * multiplier;
case a < b:
return -1 * multiplier;
case a === b:
return 0;
}
});
[].forEach.call(rows, function(row) {
tableBody.removeChild(row);
});
directions[index] = direction === "asc" ? "desc" : "asc";
newRows.forEach(function(newRow) {
tableBody.appendChild(newRow);
});
};
[].forEach.call(headers, function(header, index) {
header.setAttribute("id", index);
header.addEventListener("click", () => {
sortColumn(index, headers);
});
});
// HTML-Tabelle filtern
document.querySelectorAll('.filter-table').forEach(function(input) {
var table = document.querySelector(input.dataset.for);
var rows = table.querySelectorAll('tbody tr');
input.addEventListener('input', function(event) {
rows.forEach(function(tr) {
tr.hidden = !tr.textContent.toLowerCase().includes(input.value.toLowerCase());
});
});
})
}
</script>
</body>
</html>
daten.csv
2021-11-29 22:09;Dinslaken 2021-12-24 18:00;Weihnachten 2021-12-31 15:00;Silvester
Siehe auch: JavaScript - HTML-Tabelle sortieren, JavaScript - HTML-Tabelle filtern und CSV-Datei einlesen und dynamisch als Tabelle ausgeben.
Tabellenkalender - Blutdruckwerte
![[Bildschirmfoto]
Kalender-Tabelle [Bildschirmfoto]
Kalender-Tabelle](img/tabellenkalender_blutdruckwerte.png)
Quelltext: blutdruckwerte.php Einblenden ❘ Kopieren ❘ Link ❘ Zeilen ❘
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/*
* Tabellenkalender - Blutdruckwerte
* https://werner-zenk.de
*/
$verzeichnis = "daten.csv";
if (isset($_GET["aktualisieren"])) {
// Eintragen
if ($_POST["option"] == "eintragen") {
if (
strpos($_POST["datum"], "T") &&
!empty($_POST["owert"]) &&
!empty($_POST["uwert"]) &&
!empty($_POST["puls"])
) {
$daten = file_get_contents($verzeichnis);
$datum = str_replace("T", " ", $_POST["datum"]);
$owert = str_replace(";", "", $_POST["owert"]);
$uwert = str_replace(";", "", $_POST["uwert"]);
$puls = str_replace(";", "", $_POST["puls"]);
$daten .= PHP_EOL . $datum . ';' . $owert . ';' . $uwert . ';' . $puls;
file_put_contents($verzeichnis, $daten);
}
}
// Löschen
if ($_POST["option"] == "loeschen") {
$daten = file($verzeichnis, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$index = array_search($_POST["id"], $daten);
if ($index !== false) {
array_splice($daten, $index, 1);
file_put_contents($verzeichnis, implode(PHP_EOL, $daten));
}
}
}
// Auslesen
if (
isset($_GET["auslesen"]) ||
isset($_GET["aktualisieren"])
) {
$daten = file($verzeichnis, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$daten = array_reverse($daten);
$tabelle = '
<table id="foobar-table">
<thead>
<tr>
<th data-type="number" title="Nach Nummer sortieren">#</th>
<th data-type="number" title="Nach Datum sortieren">Datum</th>
<th data-type="number" title="Nach Uhrzeit sortieren">Uhr</th>
<th data-type="number" title="Nach oberen Wert sortieren">Oberer Wert</th>
<th data-type="number" title="Nach unteren Wert sortieren">Unterer Wert</th>
<th data-type="number" title="Nach Puls sortieren">Puls</th>
<td><input type="radio" name="id" value="0" checked="checked" title="Keinen Wert auswählen"></td>
</tr>
</thead>
<tbody>';
foreach ($daten as $nr => $data) {
list($datum, $owert, $uwert, $puls) = explode(";", $data);
sscanf($datum, "%4s-%2s-%2s %6s", $jahr, $monat, $tag, $uhr);
$tabelle .= '<tr><td>' . ($nr + 1) . '</td><td>' . $tag . '.' . $monat . '.' . $jahr . '</td><td>' . $uhr . '</td><td>' . $owert . '</td><td>' . $uwert . '</td><td>' . $puls .
'</td><td><input type="radio" name="id" value="' . $datum . ';' . $owert . ';' . $uwert . ';' . $puls . '" title="Wert auswählen"></td></tr>';
}
$tabelle .= '</tbody>
</table>';
echo $tabelle;
exit;
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Meine Blutdruckwerte</title>
<style>
body {
font-family: system-ui, -apple-system, "Segoe UI", "Roboto", "Ubuntu", "Cantarell", "Noto Sans", "Verdana", sans-serif;
}
h2 {
font-weight: Normal;
}
table#foobar-table thead {
position: sticky;
}
table#foobar-table thead {
inset-block-start: 0;
}
table#foobar-table,
table#foobar-table>thead>tr>th,
table#foobar-table>tbody>tr>td {
border: Solid 1px #888888;
padding: 5px 8px 5px 8px;
color: #000000;
}
table#foobar-table>thead>tr {
background-color: rgba(255, 202, 202, 0.8);
cursor: Pointer;
user-select: None;
}
table#foobar-table>tbody>tr:nth-child(even) {
background-color: #FFFFFF;
}
table#foobar-table>tbody>tr:nth-child(odd) {
background-color: rgb(255, 242, 242);
}
table#foobar-table th[data-type="number"]:hover,
table#foobar-table th[data-type="string"]:hover {
cursor: Pointer;
text-decoration: underline rgb(255, 38, 38);
}
table#foobar-table td {
text-align: Center;
transition: color 0.3s;
}
table#foobar-table td:hover {
color: rgb(255, 38, 38);
}
/* Spalte in der Tabelle von der Sortierung ausschließen */
table#foobar-table>thead>tr>td {
border: Solid 1px #888888;
padding: 5px 8px 5px 8px;
color: #000000;
font-weight: bold;
text-align: center;
}
input[id="filter"] {
width: 16.5rem;
}
label[for] {
display: inline-block;
width: 115px;
}
input[type="radio"] {
accent-color: rgb(255, 38, 38);
width: 15px;
height: 15px;
}
details {
margin-bottom: 10px;
}
summary {
cursor: Pointer;
user-select: None;
transition: color 0.3s;
}
summary:hover,
label:hover {
color: rgb(255, 38, 38);
}
input[type="text"],
input[type="number"],
input[type="search"],
input[type="datetime-local"],
button[type="button"] {
font-family: system-ui, -apple-system, "Segoe UI", "Roboto", "Ubuntu", "Cantarell", "Noto Sans", "Verdana", sans-serif;
border: Solid 1px #cccccc !important;
padding: 5px;
transition: box-shadow 0.3s;
}
:focus-visible {
outline: Solid 1px rgb(255, 38, 38);
}
/* Pfeil nach oben */
.upArrow::after {
content: "\2191";
color: #FFFFFF;
text-shadow: 1px 1px 0px #000000;
position: absolute;
}
/* Pfeil nach unten */
.dnArrow::after {
content: "\2193";
color: #FFFFFF;
text-shadow: 1px 1px 0px #000000;
position: absolute;
}
</style>
</head>
<body>
<h2>🩸 Meine Blutdruckwerte</h2>
<form id="form" method="post">
<p>
<label for="filter">Filter:</label>
<input type="search" class="filter-table" id="filter" data-for="#foobar-table">
</p>
<p id="ausgabe"></p>
<p>
<label for="datum">Datum:</label>
<input type="datetime-local" name="datum" id="datum" value="<?= date("Y-m-d\TH:i"); ?>">
</p>
<p>
<label for="owert">Oberer Wert:</label>
<input type="number" name="owert" id="owert">
</p>
<p>
<label for="uwert">Unterer Wert:</label>
<input type="number" name="uwert" id="uwert">
</p>
<p>
<label for="puls">Puls:</label>
<input type="number" name="puls" id="puls">
</p>
<p>
<label><input type="radio" name="option" value="eintragen" checked="checked" required="required" title="Ereignis eintragen"> Eintragen</label>  
<label><input type="radio" name="option" value="loeschen" title="Ereignis löschen"> Löschen</label>
</p>
<p><button type="button" id="button">Absenden</button></p>
</form>
<script>
// Auslesen
fetch(`${document.URL}?auslesen`, {
method: "GET",
})
.then((antwort) => {
return antwort.text();
})
.then((antwort) => {
document.getElementById("ausgabe").innerHTML = antwort;
initialisieren();
});
// Aktualisieren
document.querySelector("#button").addEventListener("click", () => {
if (document.getElementById("form").reportValidity()) {
fetch(`${document.URL}?aktualisieren`, {
method: "POST",
body: new FormData(document.getElementById("form")),
})
.then((antwort) => {
return antwort.text();
})
.then((antwort) => {
document.getElementById("ausgabe").innerHTML = antwort;
document.getElementById("form").reset();
initialisieren();
});
}
});
function initialisieren() {
// HTML-Tabelle sortieren
const table = document.getElementById("foobar-table");
const headers = table.querySelectorAll("th");
const tableBody = table.querySelector("tbody");
const rows = tableBody.querySelectorAll("tr");
const directions = Array.from(headers).map(function(header) {
return "";
});
const transform = function(index, content) {
const type = headers[index].getAttribute("data-type");
switch (type) {
case "number":
return parseFloat(content);
case "string":
default:
return content;
}
};
const sortColumn = function(index, headers) {
const cls = ["upArrow", "dnArrow"];
headers.forEach((header) => {
document.getElementById(header.id).classList.remove(...cls);
});
const direction = directions[index] || "asc";
const multiplier = (direction === "asc") ? 1 : -1;
const cssSort = (direction === "asc") ? cls[0] : cls[1];
document.getElementById(index).classList.add(cssSort);
const newRows = Array.from(rows);
newRows.sort((rowA, rowB) => {
const cellA = rowA.querySelectorAll("td")[index].textContent.toLowerCase();
const cellB = rowB.querySelectorAll("td")[index].textContent.toLowerCase();
const a = transform(index, cellA);
const b = transform(index, cellB);
switch (true) {
case a > b:
return 1 * multiplier;
case a < b:
return -1 * multiplier;
case a === b:
return 0;
}
});
[].forEach.call(rows, function(row) {
tableBody.removeChild(row);
});
directions[index] = direction === "asc" ? "desc" : "asc";
newRows.forEach(function(newRow) {
tableBody.appendChild(newRow);
});
};
[].forEach.call(headers, function(header, index) {
header.setAttribute("id", index);
header.addEventListener("click", () => {
sortColumn(index, headers);
});
});
// HTML-Tabelle filtern
document.querySelectorAll('.filter-table').forEach(function(input) {
var table = document.querySelector(input.dataset.for);
var rows = table.querySelectorAll('tbody tr');
input.addEventListener('input', function(event) {
rows.forEach(function(tr) {
tr.hidden = !tr.textContent.toLowerCase().includes(input.value.toLowerCase());
});
});
})
}
</script>
</body>
</html>
daten.csv
2022-02-05 08:04;117;95;99 2022-02-05 18:20;100;90;100