diff --git a/Zweites Jahr/README.md b/Zweites Jahr/README.md index 1c66979..f2f8a3d 100644 --- a/Zweites Jahr/README.md +++ b/Zweites Jahr/README.md @@ -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 diff --git a/Zweites Jahr/json/createJsonFile.php b/Zweites Jahr/json/createJsonFile.php new file mode 100644 index 0000000..a96ac06 --- /dev/null +++ b/Zweites Jahr/json/createJsonFile.php @@ -0,0 +1,19 @@ + "Daniel", + "nachname" => "C", + "schule" => "wvss", + "hatFuehrerschein" => true, + "alter" => 67 +]; + + +$phpJson = json_encode($phpArr); +echo "arr into json transfered
"; + +$file = file_put_contents("daniel.json", $phpJson); +echo "file created
"; \ No newline at end of file diff --git a/Zweites Jahr/json/daniel.json b/Zweites Jahr/json/daniel.json new file mode 100644 index 0000000..ec5b241 --- /dev/null +++ b/Zweites Jahr/json/daniel.json @@ -0,0 +1 @@ +{"vorname":"Daniel","nachname":"C","schule":"wvss","hatFuehrerschein":true,"alter":67} \ No newline at end of file diff --git a/Zweites Jahr/json/dbdata.json b/Zweites Jahr/json/dbdata.json new file mode 100644 index 0000000..738dd87 --- /dev/null +++ b/Zweites Jahr/json/dbdata.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/Zweites Jahr/json/fromSqlToJson.php b/Zweites Jahr/json/fromSqlToJson.php new file mode 100644 index 0000000..befb155 --- /dev/null +++ b/Zweites Jahr/json/fromSqlToJson.php @@ -0,0 +1,62 @@ +

SQL Table -> JSON

+query("SELECT * FROM $table"); + +echo "

SQL DATA

"; +echo "

Formatted

"; +echo "Vorname | Nachname | Geburtsdatum | Gehalt | Geschlecht | Einstellungsdatum
"; + +$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"]."
"; + + $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 "

JSON Array

"; +// transfer php array into json +$json = json_encode($baseJson, JSON_PRETTY_PRINT); +echo "
$json
"; + +// safe file, show error or show written bytes +$file = file_put_contents($jsonFileName, $json); +if ($file === false) { + echo "
Fehler beim Schreiben der Datei."; +} else { + echo "
dbdata.json geschrieben ($file Bytes)."; +} +?>