2
0

task 2,3: XML->Json, SQL TAble -> JSON

This commit is contained in:
cwikladaniel
2026-02-05 10:41:18 +01:00
parent 9f9625ff58
commit 21471e419e
5 changed files with 126 additions and 0 deletions

View File

@@ -7,6 +7,11 @@ Wichtige Informationen
- Wir eventuell nie aktuell sein.
- Stundenplan - [Link](https://wvss-mannheim.webuntis.com/WebUntis/?school=wvss-mannheim#/basic/timetablePublic/class?entityId=2583)
## 05-02-26 - PHP (JSON, SQL)
- Aus PHP JSON Datei erstellen
- In PHP SQL abfrage in JSON datei umwandeln
## 04-02-26 - PHP (Kryptographie)
- Caeser chiffere kontrolliert
- Vigenere chieffre gemacht

View File

@@ -0,0 +1,19 @@
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$phpArr = [
"vorname" => "Daniel",
"nachname" => "C",
"schule" => "wvss",
"hatFuehrerschein" => true,
"alter" => 67
];
$phpJson = json_encode($phpArr);
echo "arr into json transfered<br>";
$file = file_put_contents("daniel.json", $phpJson);
echo "file created<br>";

View File

@@ -0,0 +1 @@
{"vorname":"Daniel","nachname":"C","schule":"wvss","hatFuehrerschein":true,"alter":67}

View File

@@ -0,0 +1,39 @@
{
"Table": "Mitarbeiter",
"Database": "it-network",
"Host": "localhost",
"Data": [
{
"vorname": "Eva",
"nachname": "Klein",
"geburtsdatum": "1990-01-13",
"gehalt": 2000,
"geschlecht": "w",
"einstellung": "2014"
},
{
"vorname": "Kai",
"nachname": "Blei",
"geburtsdatum": "1967-10-28",
"gehalt": 3500,
"geschlecht": "m",
"einstellung": "2001"
},
{
"vorname": "Udo",
"nachname": "Ax",
"geburtsdatum": "1980-05-20",
"gehalt": 1500,
"geschlecht": "m",
"einstellung": "2001"
},
{
"vorname": "Ernst",
"nachname": "Klein",
"geburtsdatum": "1988-02-02",
"gehalt": 1000,
"geschlecht": "m",
"einstellung": "2006"
}
]
}

View File

@@ -0,0 +1,62 @@
<h1>SQL Table -> JSON</h1>
<?php
// Show Errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// config stuff
$table = "Mitarbeiter";
$dbname = "it-network";
$host = "localhost";
$jsonFileName = "dbdata.json";
// connect to db
$pdo = new PDO("mysql:host=$host;dbname=$dbname", "phpmyadmin", "server");
// get data from db
$dbData = $pdo->query("SELECT * FROM $table");
echo "<h2>SQL DATA</h2>";
echo "<h3>Formatted</h3>";
echo "Vorname | Nachname | Geburtsdatum | Gehalt | Geschlecht | Einstellungsdatum<br>";
$baseJson = [
"Table" => $table,
"Database" => $dbname,
"Host" => $host,
"Data" => []
];
// append data to json and show it
while ($row = $dbData->fetch()) {
echo $row["m_vorname"]." | ".
$row["m_nachname"]." | ".
$row["m_geburtsdatum"]." | ".
$row["m_gehalt"]." | ".
$row["m_geschlecht"]." | ".
$row["m_einstellung"]."<br>";
$baseJson["Data"][] = [
"vorname" => $row["m_vorname"],
"nachname" => $row["m_nachname"],
"geburtsdatum" => $row["m_geburtsdatum"],
"gehalt" => $row["m_gehalt"],
"geschlecht" => $row["m_geschlecht"],
"einstellung" => $row["m_einstellung"]
];
}
echo "<h2>JSON Array</h2>";
// transfer php array into json
$json = json_encode($baseJson, JSON_PRETTY_PRINT);
echo "<pre>$json</pre>";
// safe file, show error or show written bytes
$file = file_put_contents($jsonFileName, $json);
if ($file === false) {
echo "<br>Fehler beim Schreiben der Datei.";
} else {
echo "<br>dbdata.json geschrieben ($file Bytes).";
}
?>