-2

ここにアプリケーションがあります: APPLICATION

私が持っているのは、いくつかの質問であり、チェックボックス ボタンの可能な回答の各質問に関連付けられています。また、個々の回答の questionId、オプションの種類、およびマークの数を示す 3 つのテキスト入力があります。

問題が発生している場合、実際には個々の回答のマーク数です。私が試みたいのは、各質問のすべての正解について、それらが価値のあるマークの数を示す独自のテキスト入力に関連付けられていることです (以下の個人_回答テーブルにあります) 0。テキスト入力/

このサンプル アプリケーションのデータベース テーブルは次のとおりです。

質問:

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

1 に答える 1

0

まず、DB スキーマを再検討することをお勧めします。質問の DB を作成するのに必要な数よりもはるかに多くのテーブルがあり、単一の質問を取得するために必要なすべての結合などは高価な操作です。

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 と似ていますが、重要な変更が 1 つあります。すべての質問を囲むフォーム要素が 1 つしかないため、各質問のチェックボックス配列には一意の名前が必要です。私は _(questionID) を各配列に追加することを選択しました

(例: 質問 72)<input type="checkbox" name="options_72[]" id="option-D" value="D">

ヒアドキュメントを使用してループする方法は次のとおりです

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 に答える