1

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

6
  • Use file_put_contents() to save into a file Commented Jul 4, 2013 at 16:08
  • 2
    $json_string is never defined! A typo? Commented Jul 4, 2013 at 16:09
  • Presumably you want to "add" to the file, not replace the contents? It would be too bad if you lose 100 questions because you added one... Make sure you do this in a way that allows appending. Also - how do you expect to read this information back? That affects how to do this. Commented Jul 4, 2013 at 16:09
  • Another question: how did you define $count? It would definitely make sense to read all the current questions/ answers in from the file, then add new questions, and then save the entire thing back to file. That way you could even check if it's the same question (in which case maybe the answer changed). Commented Jul 4, 2013 at 16:11
  • $count is 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 Commented Jul 4, 2013 at 16:14

2 Answers 2

1

Try this piece of code. It doen't use JSON but it still saves data into a file which is split up using tab

<?php 
$count=10;//whatever you defined it to
$qa = "";
for ($x=1; $x < $count + 1; $x++) { 
    $qa .= $_POST["q".$x]."\t".$_POST["a".$x]."\n";   
}
$json_file = 'file.txt';
unlink($json_file);
file_put_contents($qa);
?>

<?php
//to retrieve q&a
$qas = file('file.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
$qas = explode("\t",$qas);
foreach($qas as $question => $answer)
{
    //your code 

}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

fwrite($handle, $encoded_array) or die('Internal Sever Error');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.