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

前端 未结 1 1533
面向向阳花
面向向阳花 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:

    What is 4+4?

    Answers: (this question is worth 2 points)

    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)

    Here's how you would loop it using a heredoc

    foreach( $_Questions AS $question ) {
      echo <<
            

    {$question['content']}

    Answers: (this question is worth {$question['marks']} points)

    EOT; $options = ['A','B','C','D','E','F']; $lastOption = substr($question['type'], -1, 1); foreach( $options as $opt ) { echo <<
    EOT; if( $opt == $lastOption ) break; } }

0 讨论(0)
提交回复
热议问题