I am making a quiz script. I want a "teacher" to be able to edit the questions. Originally, I tried to use mySQL but it was a bit difficult, so I decided to switch to a JSON file.
After doing some basic checks with PHP, I wrote this block of code
$json = array();
for ($x=1; $x < $count + 1; $x++) {
$q_and_a = array(
"Question" => $_POST["q".$x],
"Answer" => $_POST["a".$x]
);
array_push($json, $q_and_a);
}
$encoded_array = json_encode($json);
// Delete JSON file, create new one and push encoded array into file
$json_file = 'questions.json';
unlink($json_file);
$handle = fopen($json_file, 'w') or die('Cannot open file: '.$json_file);
fwrite($handle, $json_string) or die('Internal Sever Error');
After I got an internal server error, I realized that this is because the $json variable is an array; I need to convert it into a string and then insert it into the file. I first used serialize() but that just inserted some weird stuff into my file. How do I convert the json array into a string before moving it into the file?
Thanks In Advance
file_put_contents()to save into a file$json_stringis never defined! A typo?$countis supposed to be for displaying the certain number of question and answer fields. I just wanted to put it as a variable, so if I needed to use it again, I could just reference it