I have to write a php script for a university project, that takes data from a .csv file and provide it to an android app. The app visualize the data with column charts. It has a tab for the last day, and a tab which compares the last 3 days. Here's my php script so far:
<?php
if (($handle = fopen('Watt.csv', 'r')) === false) { //Open the csv file
die('Error opening file');
}
$headers = fgetcsv($handle, 4000, ';'); //Takes the csv in an array
$csv2json = array();
while ($row = fgetcsv($handle, 4000, ';')) {
$csv2json[] = array_combine($headers, $row);
}
fclose($handle);
if(isset($_GET['datum'])){ //Takes the date in the format dd/mm/jjjj
$datumApp = $_GET['datum'];
}
if(isset($_GET['uhrzeit'])){ //Takes the time in the format 1,2,3 or 4.
$uhrzeitApp = $_GET['uhrzeit'];
}
if(isset($_GET['datum01']) && isset($_GET['datum02']) && isset($_GET['datum03'])){
$datum01App = $_GET['datum01']; //Takes the 3 dates for a 3 day comparison.
$datum02App = $_GET['datum02'];
$datum03App = $_GET['datum03'];
$abfrageTage = array_multi_search($datum01App, $csvjson, "Datum"); //search through the array for the 3 days.
$abfrageTage2 = array_multi_search($datum02App, $csvjson, "Datum");
$abfrageTage3 = array_multi_search($datum03App, $csvjson, "Datum");
$abfrageTageGesamt = array_merge($abfrageTage, $abfrageTage2, $abfrageTage3);
$jsonergebnis = json_encode($abfrageTageGesamt);
echo $jsonergebnis;
}
function array_multi_search($mSearch, $aArray, $sKey = ""){ //function for searching through the array
$aResult = array();
foreach((array) $aArray as $aValues){
if($sKey === "" && in_array($mSearch, $aValues)) $aResult[] = $aValues;
else
if(isset($aValues[$sKey]) && $aValues[$sKey] == $mSearch) $aResult[] = $aValues;
}
return $aResult;
}
if ($uhrzeitApp == 1) { //If the time = 1, get the data for the time from 0-6.
$abfrage0 = array_multi_search(00, $csv2json, "Uhrzeit");
$abfrage1 = array_multi_search(01, $csv2json, "Uhrzeit");
$abfrage2 = array_multi_search(02, $csv2json, "Uhrzeit");
$abfrage3 = array_multi_search(03, $csv2json, "Uhrzeit");
$abfrage4 = array_multi_search(04, $csv2json, "Uhrzeit");
$abfrage5 = array_multi_search(05, $csv2json, "Uhrzeit");
$abfrage06 = array_merge($abfrage0, $abfrage1, $abfrage2, $abfrage3, $abfrage4, $abfrage5);
$ergebnis = array_multi_search($datum, $abfrage06, "Datum");
$jsonergebnis = json_encode($ergebnis);
echo $jsonergebnis;
}
elseif($uhrzeitApp == 2){
$abfrage6 = array_multi_search(00, $csv2json, "Uhrzeit");
$abfrage7 = array_multi_search(01, $csv2json, "Uhrzeit");
$abfrage8 = array_multi_search(02, $csv2json, "Uhrzeit");
$abfrage9 = array_multi_search(03, $csv2json, "Uhrzeit");
$abfrage10 = array_multi_search(04, $csv2json, "Uhrzeit");
$abfrage11 = array_multi_search(05, $csv2json, "Uhrzeit");
$abfrage012 = array_merge($abfrage6, $abfrage7, $abfrage8, $abfrage9, $abfrage10, $abfrage11);
$ergebnis = array_multi_search($datum, $abfrage012, "Datum");
$jsonergebnis = json_encode($ergebnis);
echo $jsonergebnis;
}
elseif($uhrzeitApp == 3){
$abfrage12 = array_multi_search(00, $csv2json, "Uhrzeit");
$abfrage13 = array_multi_search(01, $csv2json, "Uhrzeit");
$abfrage14 = array_multi_search(02, $csv2json, "Uhrzeit");
$abfrage15 = array_multi_search(03, $csv2json, "Uhrzeit");
$abfrage16 = array_multi_search(04, $csv2json, "Uhrzeit");
$abfrage17 = array_multi_search(05, $csv2json, "Uhrzeit");
$abfrage018 = array_merge($abfrage12, $abfrage13, $abfrage14, $abfrage15, $abfrage16, $abfrage17);
$ergebnis = array_multi_search($datum, $abfrage018, "Datum");
$jsonergebnis = json_encode($ergebnis);
echo $jsonergebnis;
}
elseif($uhrzeitApp == 4){
$abfrage18 = array_multi_search(00, $csv2json, "Uhrzeit");
$abfrage19 = array_multi_search(01, $csv2json, "Uhrzeit");
$abfrage20 = array_multi_search(02, $csv2json, "Uhrzeit");
$abfrage21 = array_multi_search(03, $csv2json, "Uhrzeit");
$abfrage22 = array_multi_search(04, $csv2json, "Uhrzeit");
$abfrage23 = array_multi_search(05, $csv2json, "Uhrzeit");
$abfrage024 = array_merge($abfrage18, $abfrage19, $abfrage20, $abfrage21, $abfrage22, $abfrage23);
$ergebnis = array_multi_search($datum, $abfrage024, "Datum");
$jsonergebnis = json_encode($ergebnis);
echo $jsonergebnis;
}
else{
echo "Ungültige Eingabe!";
}
?>
I take the date and the time from the app und search for it in the cvs file, then I give the correspondingly record back to the app. The time i get from the app is 1 for 0-6, 2 for 6-12, 3 for 12-18 and 4 for 18-24. If i type in the date manual, it works. But with GET i only get an empty array. I can't figure out why..
Here's an extract from my .csv file:
Datum;Uhrzeit;Strom1;Strom2;Strom3 07/12/2015;17:11;36;35;19 07/12/2015;17:17;35;33;19 07/12/2015;19:45;34;32;22 07/12/2015;19:50;34;31;20 ...
I know this is not the best way to solve this, but I'm completely new in php and i don't have to calculate with the data, so I don't change the datatypes. Does anyone know why this doesn't work? Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire