php - Read file elements into array and use as html form elements -


hi guys have been trying sometime. please give me insight on how go this. have text file containing questions , respective multiple choice answer spaced using space bar between each element. have been able read , put text file lines arrays. has prooved difficult achieve how put each , every element html form element. these codes:

text file:

number question (a) (b) (c) (d) 1 important feature of spiral model requirement analysis. risk management. quality management. configuration management. 2 worst type of coupling data coupling. control coupling. stamp coupling. content coupling. 3 1 of fault base testing techniques unit testing. beta testing. stress testing. mutation testing. 4 fault simulation testing technique mutation testing stress testing black box testing white box testing 5 rs known specification of white box testing stress testing integrated testing black box testing

the page reads text file:

`html> <head> <title>read</title> </head> <body> <b><u> questions , answers quiz</u></b <br />  <p> <?php $openfile = fopen("questionandanswers.txt", "r") or exit ("unable open text       file"); $filecontents = fread($openfile, filesize("questionandanswers.txt")); fclose($openfile); $delimiter = "  "; $myarray = explode($delimiter, $filecontents); print_r($myarray); ?> </p> </body> </html>` 

the print_r displays following:

array ( [0] => number [1] => question [2] => (a) [3] => (b) [4] => (c) [5] => (d) 1 [6] => important feature of spiral model requirement analysis. [7] => risk management. [8] => quality management. [9] => configuration management. 2 [10] => worst type of coupling [11] => data coupling. [12] => control coupling. [13] => stamp coupling. [14] => content coupling. 3 [15] => 1 of fault base testing techniques [16] => unit testing. [17] => beta testing. [18] => stress testing. [19] => mutation testing. 4 [20] => fault simulation testing technique [21] => mutation testing [22] => stress testing [23] => black box testing [24] => white box testing 5 [25] => rs known specification of [26] => white box testing [27] => stress testing [28] => integrated testing [29] => black box testing )

you explode() using space separator, that's why getting each word array element. explode() uses character give , split string whenever encounters character.

your data (the file) hasn't got pattern. need stablish rules in text, or won't able separate information want:

i modified text stablishing rules:

  1. the questions finished colon(:).
  2. the answers finished dot(.), of them last one, use number of question separator.
  3. the questions or answers won't contain numbers [0-9] or confuse regex.

this resulting text modified manually make text work:

$string = 'number question (a) (b) (c) (d) 1 important feature of spiral model is: requirement analysis. risk management. quality management. configuration management 2 worst type of coupling is: data coupling. control coupling. stamp coupling. content coupling 3 1 of fault base testing techniques is: unit testing. beta testing. stress testing. mutation testing 4 fault simulation testing technique is: mutation testing. stress testing. black box testing. white box testing 5 rs known as: specification of white box testing. stress testing. integrated testing. black box testing'; 

the solution: after can use code separate information:

<html>     <head>     <title>read</title>     </head>     <body>         <b><u> questions , answers quiz</u></b> <br /> <?php $openfile = fopen("questionandanswers.txt", "r") or exit ("unable open text       file"); $string = fread($openfile, filesize("questionandanswers.txt"));  //regex first number string's end, ignore number question... bit; preg_match('/\d.*/', $string, $match);  //we strings starting number until finds number (which beginning of question; preg_match_all('/\d\d*/', $match[0], $results);  $qas = array(); // prepare array questions/answers foreach($results[0] $result){     //separating answer string answers.     list($question, $all_answers) = explode(':', $result);      //separating different answers     $answers_array = explode('.', $all_answers);      //stuffing question , array answers prepared array;     $qas[] = array('question' => $question, 'answers' => $answers_array); }  //looping through array , outputting info form; foreach($qas $k => $v){     echo "<label>{$v['question']}</label><br/>";     echo "<select>";      //we loop through $v['answers'] because array within array answers.     foreach($v['answers'] $answer){         echo "<option>$answer</option>";//the output     }     echo "</select>";     echo "<br/><br/>"; }  ?>     </body> </html> 

looks complex because of comments, less 20 lines of text actually

you can see output here: output


notes did practice, next time try research more, , ask specific questions, or people ignore/downvote your, have read stackoverflow's faqs


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -