I'm not well conversant with java or AJAX, but how can i read each line of a string display each line of question and answers at a time, after a user have answered the question id displays the next question upto the last one. Im using

fget ()

n php to read line by line but dont know how to display one question and its respective answers at a time and be able to keep track of each submitted questions. Kindly assist with this I would highly appreciate. Here are the codes that I have:

<?php

$openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text       file");
while (!feof ($openFile))
{
$string = fgets($openFile). "<br />";

        //Regex to get from the first number to the string's end, so we ignore the Number Question... bit;

        preg_match('/\d.*/', $string, $match);

        //We get all the strings starting with a number until it finds another number (which will be the beginning of another question;

        preg_match_all('/\d\D*/', $match[0], $results);

                                                    // We prepare an array that will store the questions/answers
        $qas = array(); 
        foreach($results[0] as $result)

        //Separating the question from the string with all the answers.
        list($question, $all_answers) = explode(':', $result);

        //Separating the different answers

        $answers_array = explode('.', $all_answers);

        //Storing the question and the array with all the answers into the previously prepared array;

        $qas[] = array('question' => $question, 'answers' => $answers_array);

        //Looping through the array and outputting all the info into a form;

        $questionNr = 0;   
        foreach($qas as $k=>$v)
        {
        echo "<input id='question{$questionNr}' style='width:40%' type='text' value='".$v['question']."' name='questions[]' >";
        echo '<br /><br />';
        //Puts the answers of each question unique and in the form of radios
        foreach($v['answers'] as $answer)
       {
        echo "<input type=\"radio\" name=\"selected_answers[$questionNr]\" value=\"$answer\" />";
        echo "$answer";
        echo '<br/><br />';
        }
        $questionNr++;
        }
}

fclose ($openFile);
?>
<input name= "submit" type="submit" value="Submit Answers">  <input type="reset" value ="Clear Answers">
</form>
</body>
</html>

Recommended Answers

All 9 Replies

You will have to store the current question number, for example in a $_SESSION variable.

Thank you pirataeas for checking on this, How will I implement the sessions with what I have currently?

String = "This is my Question"
$_SESSION[String_variable];

Its like this to store a string to session.
Happy coding.

Thank you guys imBaCodes and pritaeas, but what I am actually talking about is how to display my form that contains the questions and answers on the screen such that only one question appears on the screen at a time. Like in this case here Click Here

Member Avatar for diafol

For this you can either do ajax where you retrieve a question from the server one at a time, or you can retrieve the whole info on page load and just use javascript to retrieve the data (from a js array/ json). For the first one, you could use session variables to store answers (e.g. an array) and for the second one, you could store the answers in js array. Wen you get to the end of the test, the session array (or js array) can be processed to get the total score and you can then compare the answers with the correct ones.

Thank you diafol, Unfortunately for me I am Totally out of context when it comes to ajax and javascript. I am sorry if this sound absurd, but could you kindly assist on the same? Thank you.

Member Avatar for diafol

OK, where do you want to start?

If you want the Ajax (get questions one at a time)...
I suggest you have a look at jQuery to begin with as it hides the complexity of using ajax very well and ensures cross-browser niceness. Start with a simple example and think about how you could wrestle your data into an ajax context. Have a little play with it.

For just a plain js solution, this could be a little bit more tricky if you're new to js, even though it's a simpler procedure as the server (php) isn't involved until the end of the quiz.

You get the data from the DB into a php array using a while loop. SO let's assume all the question and correct answer data is now in an array called $qa.

You now need to convert it into a format that js can understand. This can be done easily with json_encode() [look it up in the php.net manual]. Let's now assume that the json'ed data is now in a variable called $json.

Next you hand off the data to js:

<script>
    var data = <?php echo $json;?>; //notice no quotes around the php 
    ...
</script>

Then you can navigate through the data variable to get the next question and answers and display them to the appropriate place, e.g. a <div> or a radiobutton <input ... />

But that's just the display bit, you then have to store the user's responses. Again this is fairly straightforward for somebody with some experience of js. You can set up an array, let's call it 'responses'. ANd for each answer submitted, you can 'push' an item into the array.

At the end of the quiz, a button or something should appear in order to submit the responses. You use js to hijack the submit event - do a little js manipulation. Your choice as to how to proceed from here are many-fold. You could pass the js array via ajax, create a hidden field in the form and pass the data into that...

ANyway, I realise I'm giving you glimpses of things without too much concrete code. I suggest you take your time and have a good root around js and ajax (possibly using jQuery) and get a good feel for what it can do for you.

Lets assume that I already have the array that stores both questipons and answers in my line 25

$qas[] = array('question' => $question, 'answers' => $answers_array);

Can I commence the Jquery from there?

Member Avatar for diafol

Yes, that should be OK. Use the json_encode if using the js-only solution. Perhaps it would be a good idea to have the question_id in the array too, so you can use this as a hook. If using the ajax solution, you only need to retrieve one question at a time - therefore the question_id (question number) will be important.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.