-2

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.

Jean
  • 1
  • 3
  • Can you just post an example of what `$_POST` contains via `var_dump($_POST);`? The way you are constructing variable variables with `${..}` is likely a recipe for madness :) It would be much easier to manipulate those as array keys onto an array in the first place then to create a bunch of global scope variables, and I suspect there is a much more direct way of transforming `$_POST` into the desired structure for json output. – Michael Berkowski Dec 02 '18 at 22:16
  • All $_POST vars are just int values. I do agree that I am probably complicating things... While I will try changing them into arrays from the start which will probably be good, I don't understand why global variables should be used? I am adding a variable which is unneeded. I make $_POST into a var then place it into an array. Thanks for your comment. – Jean Dec 03 '18 at 15:32
  • Actually the last to post value will be strings in the future – Jean Dec 03 '18 at 15:38

1 Answers1

0

This is in no way pretty and overcomplicates things but this is does work. Don't bother unless you have a similair problem.

$arrWeek = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saterday","Sunday");
$arrWeekSimple = array("Mo","Tu","We","Tu","Th","Fr","Sa","Su");
//$mowakeh = $_POST['mowakeh'];



$strName = "Wake";
$arrContent=array();
$arrAlarms=array("id"=>1,"name"=>$strName);
/*

$arrContents=[0]Monday=MOM,MOH
*/
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 would look like
$intMoH = 20;
$intMoM = 30;
$strMoAction = 5;
$strMoSource = 7;
*/
echo "<br>Day?";
echo $arrWeek[$i];
${$arrWeek[$i]}= array($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"});
echo "<br>For this day array  ";
print_r(${$arrWeek[$i]});

//$strCurrentDay=$arrWeek[$i];
$arrContent[$arrWeek[$i]]=${$arrWeek[$i]};
//array_push($arrContent,${$arrWeek[$i]});
echo "<br>arrcontent  ";
print_r($arrContent);

}
$arrAlarms["content"]=$arrContent;

echo "Array made <br>";
print_r($arrAlarms);

The result would be this:

Day?Monday
For this day array Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) )
Day?Tuesday
For this day array Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) )
Day?Wednesday
For this day array Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Wednesday] => Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 ) )
Day?Thursday
For this day array Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Wednesday] => Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 ) [Thursday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) )
Day?Friday
For this day array Array ( [ThH] => 5 [ThM] => 5 [ThAction] => 5 [ThSource] => 5 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Wednesday] => Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 ) [Thursday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Friday] => Array ( [ThH] => 5 [ThM] => 5 [ThAction] => 5 [ThSource] => 5 ) )
Day?Saterday
For this day array Array ( [FrH] => 6 [FrM] => 6 [FrAction] => 6 [FrSource] => 6 )
arrcontent Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Wednesday] => Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 ) [Thursday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Friday] => Array ( [ThH] => 5 [ThM] => 5 [ThAction] => 5 [ThSource] => 5 ) [Saterday] => Array ( [FrH] => 6 [FrM] => 6 [FrAction] => 6 [FrSource] => 6 ) ) Array made
Array ( [id] => 1 [name] => Wake [content] => Array ( [Monday] => Array ( [MoH] => 1 [MoM] => 1 [MoAction] => 1 [MoSource] => 1 ) [Tuesday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Wednesday] => Array ( [WeH] => 3 [WeM] => 3 [WeAction] => 3 [WeSource] => 3 ) [Thursday] => Array ( [TuH] => 4 [TuM] => 4 [TuAction] => 4 [TuSource] => 4 ) [Friday] => Array ( [ThH] => 5 [ThM] => 5 [ThAction] => 5 [ThSource] => 5 ) [Saterday] => Array ( [FrH] => 6 [FrM] => 6 [FrAction] => 6 [FrSource] => 6 ) ) ) 

A few things to keep in mind: array_push() doesn't allow you to set a key. ${$arrWeek[$i]} is a array that gets assigned to a different array where the key is $arrWeek[$i]. When making an array with $arr=array("id"=>1,"name"=$name) don't stupidly forget a that it always is $arr=array("id"=>1,"name"=>$name). Also an id key must be a string.

Sources https://www.tutorialspoint.com/php/php_get_post.htm How to push both value and key into array

Jean
  • 1
  • 3