How to associate each option button with their own individual marks?

前端 未结 1 1519
面向向阳花
面向向阳花 2021-01-29 11:51

I have an application here: APPLICATION

What I have is some questions and associated with each questions in their possible answers in checkbox buttons, and three text in

相关标签:
1条回答
  • 2021-01-29 12:15

    First, I would like to recommend that you reconsider your DB schema. You have far more tables than you need to create a DB of questions, and all the joins etc. you need to retrieve a single question are expensive operations.

    Let's say you want your HTML to look something like the following:

    <div class="queWrap" id="question-72">
        <h2 class="question-text">What is 4+4?</h2>
        <h3>Answers: <span class="questionMarks">(this question is worth 2 points)</span></h3>
    
        <div class="ck-button">
            <label class="fixedLabelCheckbox">
                <input type="checkbox" name="options_72[]" id="option-A" value="A">
                <span>0</span>
            </label>
        </div>
    
        <div class="ck-button">
            <label class="fixedLabelCheckbox">
                <input type="checkbox" name="options_72[]" id="option-B" value="B">
                <span>4</span>
            </label>
        </div>
    
        <div class="ck-button">
            <label class="fixedLabelCheckbox">
                <input type="checkbox" name="options_72[]" id="option-C" value="C">
                <span>8</span>
            </label>
        </div>
    
        <div class="ck-button">
            <label class="fixedLabelCheckbox">
                <input type="checkbox" name="options_72[]" id="option-D" value="D">
                <span>16</span>
            </label>
        </div>
    </div>
    

    Now let's say your query's result->fetch() above was rewritten more smartly like so:

    $_Questions = array();
    while( $qandaqrystmt->fetch() ) {
       $_Questions[] = array('id'=>$qandaQuestionId,'num'=>$qandaQuestionNo,'content'=>$qandaQuestionContent,'type'=>$qandaOptionType,'answer'=>$qandaAnswer,'marks'=>$qandaAnswerMarks);
    }
    

    Then to output the questions we just want to loop this and generate the appropriate HTML. I want to point out that it would be incredibly stupid to send the correct answer along with the question to the test taker, even if it is hidden within the html. You'll note that this HTML is similar to your own, but has one alteration that is CRITICAL to make: because you only have ONE form element surrounding all of your questions, each question's checkbox array needs a unique name. I've chosen to append _(questionID) to each array like so

    (e.g. question 72) <input type="checkbox" name="options_72[]" id="option-D" value="D">

    Here's how you would loop it using a heredoc

    foreach( $_Questions AS $question ) {
      echo <<<EOT
        <div class="queWrap" id="question-{$question['id']}">
            <h2 class="question-text">{$question['content']}</h2>
            <h3>Answers: <span class="questionMarks">(this question is worth {$question['marks']} points)</span></h3>
        EOT;
    
    $options = ['A','B','C','D','E','F'];
    $lastOption = substr($question['type'], -1, 1);
    foreach( $options as $opt ) {
      echo <<<EOT
        <div class="ck-button">
            <label class="fixedLabelCheckbox">
                <input type="checkbox" name="options_{$questions['id']}[]" value="$opt">
                <span>$opt</span>
            </label>
        </div>
        EOT;
      if( $opt == $lastOption )
        break;
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题