I have this text file that contain questions and their respective four multiple answers, when I try to display them in html form, the diplay only contains three multiple answers and not four. this are the codes:
Text file:

$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 8)  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 code that display them in a form format:

    <title>Question and Answer Read From Text File</title>
        <form action="processor.php" method="POST" align="center">
        <b><u> QUESTIONS AND ANSWERS QUIZ</u></b> <br /><br />
$openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text       file");
$string = fread($openFile, filesize("questionandanswers.txt"));

//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);

$qas = array(); // We prepare an array with all the questions/answers
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);

    //Stuffing 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;
foreach($qas as $k=>$v)
echo "<input id='text_disabled' style='width:40%' type='text' value='".$v['question']."' name='questions[]' disabled>";
echo "<select name='selected_answers[]>";
foreach($v['answers'] as $answer){
echo "<option value='".$answer."'>".$answer."</option>";
echo "</select>";
echo "<br/><br/>";
<input type="submit" value="Submit Answers">  <input type="reset" value ="Clear Answers">

Where could the problem be in my code?

Recommended Answers

All 5 Replies

Line 41 :-)

echo "<select name='selected_answers[]>";

Sometimes these things are easier to spot when using the alternate PHP syntax - as your IDE is more likely to highlight better.

<?php foreach($qas as $v): ?>
    <p><input id="text_disabled" style="width:40%" type="text" value="<?php echo $v['question']; ?>" name="questions[]" disabled />
    <select name="selected_answers[]">
        <?php foreach($v['answers'] as $answer): ?>
            <option value="<?php echo $answer; ?>"><?php echo $answer; ?></option>
        <?php endforeach; ?>
<?php endforeach; ?>
<p><input type="submit" value="Submit Answers">  <input type="reset" value ="Clear Answers"></p>

Thank you for that, One more thing, how do i get the select box to be null before a user selects an option. because when I display the form, the select already has one answer in the pull down menu. Thank you.

Like in the following:

echo "<option value=''>".$answer."</option>";

Such that until a user selects the answer, null can also be an answer?

Simply add this after the <select>

    <option value="">Select Answer...</option>

above line 4 in my code snippet.

Be a part of the DaniWeb community

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