如何将每个选项按钮与其各自的标记关联起来? [英] How to associate each option button with their own individual marks?

查看:72
本文介绍了如何将每个选项按钮与其各自的标记关联起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个应用程序:应用程序

I have an application here: APPLICATION

我所遇到的是一些问题,并在复选框按钮中列出了与每个问题相关的可能答案,以及三个文本输入,分别显示questionId,选项类型和每个答案的分数.

What I have is some questions and associated with each questions in their possible answers in checkbox buttons, and three text inputs showing questionId, option type and number of marks for each individual answer.

如果我遇到问题,则每个答案的分数实际上是多少.我想尝试的是,对于每个问题的每个正确答案,它们都与自己的文本输入相关联,该文本输入显示了它们值得的分数数量(在下面的Individual_Answer表中找到),否则对于所有错误的答案,它们都值得在其文本输入中/

Actually the number of marks for each individual answer if the problem I am getting. What I want to attempt is that for every correct answer for each question, they are associated with their own text input showing the number of marks they are worth (found in Individual_Answer Table below) else for all incorrect answers, they are all worth 0 in their text inputs/

现在是此示例应用程序的数据库表:

Now here is the database tables for this example application:

问题:

QuestionId (PK auto)  QuestionNo  SessionId (FK Session) OptionId (FK Option)    
72                    1           26                     3
73                    2           26                     4

Option_Table:

OptionId (PK Auto)  OptionType
1                   A-C
2                   A-D
3                   A-E
4                   A-F

答案:

AnswerId (PK auto)    QuestionId (FK Question)      Answer  
1                          72                         C             
2                          73                         A             
3                          73                         C             
4                          73                         D    

个人答案:

AnswerId (PK auto)  AnswerMarks
1                   2
2                   2
3                   1
4                   2

实际代码如下:

//$qandaqry query is here and executed


        $qandaqrystmt->bind_result($qandaQuestionId,$qandaQuestionNo,$qandaQuestionContent,$qandaOptionType,$qandaAnswer,$qandaAnswerMarks );

        $arrQuestionId = array();
        $arrQuestionNo = array();
        $arrQuestionContent = array();
        $arrOptionType = array();
        $arrAnswer = array();
        $arrAnswerMarks = array();

        while ($qandaqrystmt->fetch()) {
        $arrQuestionId[ $qandaQuestionId ] = $qandaQuestionId; //QuestionId
        $arrQuestionNo[ $qandaQuestionId ] = $qandaQuestionNo; //QuestionNo
        $arrQuestionContent[ $qandaQuestionId ] = $qandaQuestionContent; //QuestionContent
        $arrOptionType[ $qandaQuestionId ] = $qandaOptionType; //OptionType
        $arrAnswer[ $qandaQuestionId ] = $qandaAnswer; //Answer
        $arrAnswerMarks[ $qandaQuestionId ] = $qandaAnswerMarks; //AnswerMarks
      }


    ?>
    <form action='results.php' method='post' id='exam'>

    <?php

//Retrieve options for each question

    function ExpandOptionType($option) { 
        $options = explode('-', $option);
        if(count($options) > 1) {
            $start = array_shift($options);
            $end = array_shift($options);
            do {
                $options[] = $start;
            }while(++$start <= $end);
         }
         else{
            $options = explode(' or ', $option);
         }
         echo '<p>';
         foreach($options as $indivOption) {
             echo '<div class="ck-button"><label class="fixedLabelCheckbox"><input type="checkbox" name="options[]" id="option-' . $indivOption . '" value="' . $indivOption . '" /><span>' . $indivOption . '</span></label></div>';
         }
          echo '</p>';


    }


    foreach ($arrQuestionId as $key=>$question) {

    ?>

    <div class="queWrap">

//Each QuestionNo and QuestionContent
    <p><?php echo htmlspecialchars($arrQuestionNo[$key]) . ": " .  htmlspecialchars($arrQuestionContent[$key]); ?></p>

//Output each Individual Option
    <p><?php echo ExpandOptionType(htmlspecialchars($arrOptionType[$key])); ?></p>

//Output each QuestionId text input per question
    <p>Question Id:<input type='text' class='questionIds' name='questionids' value='<?php echo htmlspecialchars($arrQuestionId[$key]); ?>' /></p>

//Output each OptionType text input per question
    <p>Option Type: <input type='text' class='optionType' name='optiontype' value='<?php echo htmlspecialchars($arrOptionType[$key]); ?>' /></p>

//Output each AnswerMarks per answer in each question
    <p>Each Answer's Marks<input type='text' class='answermarks' name='answerMarks' value='<?php echo htmlspecialchars($arrAnswerMarks[$key]); ?>' /></p>

    </div>


    <?php

    }

    ?>
    </form>

推荐答案

首先,我建议您重新考虑数据库架构.您拥有的表远远多于创建问题数据库所需的表,并且所有联接等.检索单个问题所需的操作都是昂贵的操作.

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.

假设您希望您的HTML看起来像以下内容:

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>

现在,假设您的查询result->fetch()上面的代码更聪明地重写为:

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

然后输出我们仅想循环的问题并生成适当的HTML.我想指出的是,即使将正确的答案和问题一起发送给考生,即使将其隐藏在html中,也是非常愚蠢的.您会注意到,此HTML与您的HTML相似,但要进行的一项关键更改是:由于所有问题都只有一个表单元素,因此每个问题的复选框数组都需要一个唯一的名称.我选择将_(questionID)附加到每个数组中,例如

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

(例如问题72)<input type="checkbox" name="options_72[]" id="option-D" value="D">

以下是使用 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;
}

}

这篇关于如何将每个选项按钮与其各自的标记关联起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆