Ferdinand Posted May 7, 2013 Share Posted May 7, 2013 '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 () in 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> Quote Link to comment https://forums.phpfreaks.com/topic/277740-read-each-line-of-string-and-display/ Share on other sites More sharing options...
Yohanne Posted May 7, 2013 Share Posted May 7, 2013 try to use if statement and php operator Quote Link to comment https://forums.phpfreaks.com/topic/277740-read-each-line-of-string-and-display/#findComment-1428809 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 I think it would help if you gave a sample from your txt file, so that we can see the format Quote Link to comment https://forums.phpfreaks.com/topic/277740-read-each-line-of-string-and-display/#findComment-1428810 Share on other sites More sharing options...
Ferdinand Posted May 8, 2013 Author Share Posted May 8, 2013 Here is the text file file I am using: $string = 1) The most important feature of spiral model is: requirement analysis. Risk management. quality management. configuration management 2) The worst type of coupling is: Data coupling. control coupling. stamp coupling. content coupling 3) One of the fault base testing techniques is: Unit testing. beta testing. Stress testing. mutation testing 4) A fault simulation testing technique is: Mutation testing. Stress testing. Black box testing. White box testing 5) RS is also known as specification of: White box testing. Stress testing. Integrated testing. Black box testing 6) Which of these terms is a level name in the Capability Maturity Model?: Ad hoc. Repeatable. Reusable. Organized 7) FP-based estimation techniques require problem decomposition based on?: Information domain values. project schedule. software functions. process activities What types of models are created during software requirements analysis?: Functional and behavioral. Algorithmic and data structure. Architectural and structural. Usability and reliability 9) Which of the following is not an attribute of software engineering: Efficiency. Scalability. Dependability. Usability And the guidelines that I used to guide me in the text file. The questions will be finished with a colon(. The answers will be finished with a dot(.), all of them but the last one, as we will use the number of the question as separator. The questions or answers won't contain any numbers [0-9] or it will confuse the regex. Quote Link to comment https://forums.phpfreaks.com/topic/277740-read-each-line-of-string-and-display/#findComment-1429035 Share on other sites More sharing options...
Ferdinand Posted May 8, 2013 Author Share Posted May 8, 2013 I am talking about something like this http://quiz.elanman.com/ Quote Link to comment https://forums.phpfreaks.com/topic/277740-read-each-line-of-string-and-display/#findComment-1429130 Share on other sites More sharing options...
jazzman1 Posted May 10, 2013 Share Posted May 10, 2013 Just create an php array (or js) with all posible right answers. When the new answer has been submited just check if this answer exist in that answers array. Then, define a new $_SESSION['results'] array setting up to 0 or NULL ($_SESSION['results'] = 0 ), so If the answer is correct set that session $_SESSION['results'] = $_SESSION['results'] + 1. Finally count the indexes of that array, display the results and unset($_SESSION['results']) That's all PS: Don't forget to use a session_start(), check the php.net documentation. Quote Link to comment https://forums.phpfreaks.com/topic/277740-read-each-line-of-string-and-display/#findComment-1429420 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.