task 2,3: XML->Json, SQL TAble -> JSON
This commit is contained in:
19
Zweites Jahr/json/createJsonFile.php
Normal file
19
Zweites Jahr/json/createJsonFile.php
Normal 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>";
|
||||
1
Zweites Jahr/json/daniel.json
Normal file
1
Zweites Jahr/json/daniel.json
Normal file
@@ -0,0 +1 @@
|
||||
{"vorname":"Daniel","nachname":"C","schule":"wvss","hatFuehrerschein":true,"alter":67}
|
||||
39
Zweites Jahr/json/dbdata.json
Normal file
39
Zweites Jahr/json/dbdata.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
62
Zweites Jahr/json/fromSqlToJson.php
Normal file
62
Zweites Jahr/json/fromSqlToJson.php
Normal 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).";
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user