To sum up the project; i am building a webpage where you can enter alarm times and the action to do when the time is met. There is my website script and a python script. The python script checks against a config file if the time conditions are met and then performs the required actions.
After collecting all needed information, I am having trouble placing it into a json file. The json config file gets overwritten everytime a user enters or changes a value and submits it. (To keep previous alarm times they are placed in the value of the input from the form) The alarm times can be different every day and are then repeated weekly. To specify, I make a lot of variables with all the different values for every time entered. For monday I would have
$intMoH
$intMoM
$intMoAction
$intMoSource
this is repeated for all days of the week.
I then want to place them all into an array so that they can be encoded and changed into json format.
The json format is supposed to resemble something like this.
[{
"wake": [{
"id": 1,
"MoM": 8,
"MoH": 30,
"MoAction": "music",
"MoSource": "music.mp3",
"TuM": 7,
"TuH": 30,
"TuAction":"music",
"TuSource":"music2.mp3"
}]
}]
I don't make a new element called Mo and then put all the items as its child because I was told that that would make putting it into an array easier (less dimensions).
My php page builds a form and then submits to itself (without using ajax, simply because I don't know it).
$arrWeek = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saterday","Sunday");
$arrWeekSimple = array("Mo","Tu","We","Tu","Th","Fr","Sa","Su");
//$mowakeh = $_POST['mowakeh'];
$strName = "wake";
for( $i = 0; $i<6; $i++ )
{
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"int" . $arrWeekSimple[$i] ."H"} = $_POST[$tempPost];
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"int" . $arrWeekSimple[$i] ."M"} = $_POST[$tempPost];
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"str" . $arrWeekSimple[$i] ."Action"} = $_POST[$tempPost];
$tempPost = "frm". $arrWeekSimple[$i] ."H";
${"str" . $arrWeekSimple[$i] ."Source"} = $_POST[$tempPost];
/*
What values after taken from form would look like
$intMoH = 20;
$intMoM = 30;
$strMoAction = 5;
$strMoSource = 7;
*/
$arrNewJson=array();
$arrPush = array($strName=array(id=>1, $arrWeekSimple[$i]."H"=>${"int".$arrWeekSimple[$i]."H"}, $arrWeekSimple[$i]."M"=>${"int".$arrWeekSimple[$i]."M"}, $arrWeekSimple[$i]."Action"=>${"str".$arrWeekSimple[$i]."Action"},$arrWeekSimple[$i]."Source"=>${"str".$arrWeekSimple[$i]."Source"})
array_push($arrNewJson,$arrPush);
}
echo "Array made <br>";
print_r($arrNewJson);
$toTransfer = json_encode($arrNewJson, true);
file_put_contents("config.json",$toTransfer);
What I struggle with is taking all the the information placing it into an multidimensional array to then be able to encode it to json format and replace the file. How do I go through each day and add that to the array? After research it seems that because of the loop I should use array_push but just can't seem to understand how to use it. I wonder also if in the json file it would be easier to go another dimension and add the days of the week?
Any help is apreciated. I hope all of this is understandable :-) Thank you for your time.