-2

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

我所拥有的是一些问题,并与复选框按钮中可能的答案中的每个问题相关联,以及三个文本输入显示 questionId、选项类型和每个单独答案的标记数。

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

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

问题:

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

选项表:

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>
4

1 回答 1

0

首先,我想建议您重新考虑您的数据库架构。您拥有的表比创建问题数据库所需的要多得多,而且检索单个问题所需的所有连接等都是昂贵的操作。

假设您希望您HTML的外观如下所示:

<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()上面被更巧妙地重写,如下所示:

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

然后输出问题,我们只想循环它并生成适当的 HTML。我想指出,将正确答案连同问题一起发送给应试者是非常愚蠢的,即使它隐藏在 html 中。您会注意到此 HTML 与您自己的 HTML 相似,但有一个非常重要的更改:因为您只有一个表单元素围绕所有问题,所以每个问题的复选框数组都需要一个唯一的名称。我选择像这样将 _(questionID) 附加到每个数组

(例如问题 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;
}

}
于 2013-02-12T20:41:17.070 回答