change to SQL

This commit is contained in:
schrom01 2023-02-15 13:31:48 +01:00
parent 20618367e0
commit 67c5550de6
2 changed files with 21 additions and 33 deletions

View File

@ -13,15 +13,22 @@ function objectToArray($object, $fullJSON) {
$object -> countWatchD];
}
function findRowIndex($id, $data) {
$rowCounter = -1;
foreach ($data as $row) {
$rowCounter ++;
if($row[0] == $id) {
return $rowCounter;
}
}
return $rowCounter + 1;
$databaseAddress = "127.0.0.1";
$databasePort = "3306";
$databaseName = "u517357132_genderwatchpro";
$databaseUser = "u517357132_genderwatchpro";
$databasePassword = "zU!7gRHA6x";
$mysqli = new mysqli($databaseAddress, $databaseUser, $databasePassword, $databaseName);
$mysqli = new mysqli($databaseAddress, $databaseUser, $databasePassword, $databaseName);
function saveToDatabase($contentArray, $mysqli) {
$mysqli->query("INSERT INTO `u517357132_genderwatchpro` (`id`, `fullJSON`, `timeWatchM`,`timeWatchW`, `timeWatchD`, `countWatchM`, `countWatchW`, `countWatchD`) values ($contentArray[0], $contentArray[2], $contentArray[3], $contentArray[4], $contentArray[5], $contentArray[6], $contentArray[7], $contentArray[8]) ON DUPLICATE KEY UPDATE `id` = $contentArray[0]");
}
function getFromDatabase() {
}
$entityBody = '{"lastSave": 0, "timeWatchM": 0, "timeWatchW": 0, "timeWatchD": 0, "countWatchM": 0, "countWatchW": 0, "countWatchD": 0, "id":"test","watches":[{"actions":[]},{"actions":[]},{"actions":[]}]}';
@ -32,20 +39,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo "POST Request received.<br>";
if(property_exists($entityBodyObject, "id")) {
$data = read_xls_file($fileName);
foreach ($data as $row) {
echo "new Row: <br>";
foreach ($row as $cell) {
echo $cell." ";
}
}
$rowIndex = findRowIndex($entityBodyObject->id, $data);
$data[$rowIndex] = objectToArray($entityBodyObject, $entityBody);
write_xlsxFile($fileName, $data);
saveToDatabase(objectToArray($entityBodyObject, $entityBody), $mysqli);
echo "<br>Data saved.<br>";
}
} elseif($_SERVER['REQUEST_METHOD'] === 'GET') {
download_xlsxFile(getFromDatabase());
} else {
echo "invalid Request.<br> Type: ".$_SERVER['REQUEST_METHOD'];
}

View File

@ -1,19 +1,9 @@
<?php
require $_SERVER['DOCUMENT_ROOT'] . '/spreadSheetReader/SimpleXLSX.php';
require $_SERVER['DOCUMENT_ROOT'] . '/spreadSheetReader/SimpleXLSXGen.php';
function read_xls_file($filename){
$fileContent = [];
if ($xlsx = SimpleXLSX::parse($filename)) {
$fileContent = ($xlsx->rows());
} else {
echo SimpleXLSX::parseError();
}
return $fileContent;
}
function write_xlsxFile($filename, $data) {
function download_xlsxFile($data) {
$xlsx = SimpleXLSXGen::fromArray($data);
$xlsx->saveAs($filename);
$xlsx->downloadAs("data.xlsx");
}