【问题标题】:Differentiate Choices with Correct Answer using PHP使用 PHP 区分选择与正确答案
【发布时间】:2017-07-23 15:45:28
【问题描述】:

将有 4 个选项 选项1,选项2,选项3,选项4

(A) Any student who does not have the power to learn in a group is almost always unsuccessful 

(B) EDPeer group pressure and mutual understanding of any concept develops better understanding among students 

(C) ESAny student who does not have the power to learn in a group is almost always unsuccessful 

(D) Peer group pressure and mutual understanding of any concept develops better understanding among students

$single_answer1="ESA 任何没有能力学习的学生 一个小组几乎总是不成功的”。

$single_answer1 是学生选择的答案。

从上述选项中,“option1”是正确答案,但学生选择“option3”。

我必须区分像 for
背景为绿色的“option1”
“option3”,背景颜色为红色

如果有时 option1 和 answer 两者相等,则将边框颜色设为绿色。

如果 single_answer 为“未定义”或空值,则它将显示黄色背景的正确答案(未选择任何内容和正确答案)。

<?php if ($option1 == $single_answer1) { ?>
    <label style="font-size:14px;background-color:lightgray;border:1px solid green;padding:10px" >
        <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
        <span class="">(A) <?php echo $option1; ?>
    </label>
<?php } else if ($single_answer1 != $option1) {
    ?>
    <label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
        <label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
        <span class="">(A) <?php echo $option1; ?>
    </label>
<?php } else if ($single_answer1 == 'undefined') { ?>
    <label style="font-size:14px;background-color:yellow;border:1px solid green;padding:10px" >
        <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
        <span class="">(A) <?php echo $option1; ?>
    </label>
<?php } else { ?>
    <label style="font-size:14px">
        <span class=""><input type="radio" name="single_question_answer1" value="<?php echo $option1; ?>" ></span> (A) <?php echo $option1; ?>
    </label>
<?php } ?><br/><br/>

我试过这样,但我得到错误的输出请任何人帮助我摆脱这个问题。在此先感谢。

【问题讨论】:

    标签: php css radio-button


    【解决方案1】:

    首先改变你的洗牌逻辑,

    $a = [$option1, $option2, $option3, $option4];
    shuffle($a);
    $b      = ['option1', 'option2', 'option3', 'option4'];
    $result = array_combine($b, $a);
    extract($result);
    

    这是你的代码,

    if ($option1 == $correct_answer && $correct_answer == $single_answer1) {
        $sc_flag = 1; // student's selected correct flag
    }
    if ($option2 == $correct_answer && $correct_answer == $single_answer1) {
        $sc_flag = 2;
    }
    if ($option3 == $correct_answer && $correct_answer == $single_answer1) {
        $sc_flag = 3;
    }
    if ($option4 == $correct_answer && $correct_answer == $single_answer1) {
        $sc_flag = 4;
    }
    if ($option1 == $correct_answer) {
        $correct_flag = 1; // correct answer
    }
    if ($option2 == $correct_answer) {
        $correct_flag = 2;
    }
    if ($option3 == $correct_answer) {
        $correct_flag = 3;
    }
    if ($option4 == $correct_answer) {
        $correct_flag = 4;
    }
    if ($option1 == $single_answer1) {
        $actual_flag = 1; // selected option by student
    }
    if ($option2 == $single_answer1) {
        $actual_flag = 2;
    }
    if ($option3 == $single_answer1) {
        $actual_flag = 3;
    }
    if ($option4 == $single_answer1) {
        $actual_flag = 4;
    }
    ?>
    <?php if ($sc_flag == 1) {?>
        <label style="font-size:14px;background-color:lightgray;border:1px solid green;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
            <span class="">(A) <?php echo $option1; ?>
        </label>
    <?php } else if ($actual_flag == 1 && $correct_flag != 1) {
        ?>
        <label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
            <span class="">(A) <?php echo $option1; ?>
        </label>
    <?php } else if ($correct_flag == 1) {?>
        <label style="font-size:14px;background-color:yellow;border:1px solid yellow;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:yellow;font-size:15px"></i></label>
            <span class="">(A) <?php echo $option1; ?>
        </label>
    <?php } else {?>
        <label style="font-size:14px">
            <span class=""><input type="radio" name="single_question_answer1" value="<?php echo $option1; ?>" ></span> (A) <?php echo $option1; ?>
        </label>
    <?php }?><br/><br/>
    <?php if ($sc_flag == 2) {?>
        <label style="font-size:14px;background-color:lightgray;border:1px solid green;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
            <span class="">(B) <?php echo $option2; ?>
        </label>
    <?php } else if ($actual_flag == 2 && $correct_flag != 2) {
        ?>
        <label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
            <span class="">(B) <?php echo $option2; ?>
        </label>
    <?php } else if ($correct_flag == 2) {?>
        <label style="font-size:14px;background-color:yellow;border:1px solid yellow;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:yellow;font-size:15px"></i></label>
            <span class="">(B) <?php echo $option2; ?>
        </label>
    <?php } else {?>
        <label style="font-size:14px">
            <span class=""><input type="radio" name="single_question_answer1" value="<?php echo $option2; ?>" ></span> (B) <?php echo $option2; ?>
        </label>
    <?php }?><br/><br/>
    <?php if ($sc_flag == 3) {?>
        <label style="font-size:14px;background-color:lightgray;border:1px solid green;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
            <span class="">(C) <?php echo $option3; ?>
        </label>
    <?php } else if ($actual_flag == 3 && $correct_flag != 3) {
        ?>
        <label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
            <span class="">(C) <?php echo $option3; ?>
        </label>
    <?php } else if ($correct_flag == 3) {?>
        <label style="font-size:14px;background-color:yellow;border:1px solid green;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
            <span class="">(C) <?php echo $option3; ?>
        </label>
    <?php } else {?>
        <label style="font-size:14px">
            <span class=""><input type="radio" name="single_question_answer1" value="<?php echo $option3; ?>" ></span> (C) <?php echo $option3; ?>
        </label>
    <?php }?><br/><br/>
    <?php if ($sc_flag == 4) {?>
        <label style="font-size:14px;background-color:lightgray;border:1px solid green;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
            <span class="">(D) <?php echo $option4; ?>
        </label>
    <?php } else if ($actual_flag == 4 && $correct_flag != 4) {
        ?>
        <label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
            <span class="">(D) <?php echo $option4; ?>
        </label>
    <?php } else if ($correct_flag == 4) {?>
        <label style="font-size:14px;background-color:yellow;border:1px solid green;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
            <span class="">(D) <?php echo $option4; ?>
        </label>
    <?php } else {?>
        <label style="font-size:14px">
            <span class=""><input type="radio" name="single_question_answer1" value="<?php echo $option4; ?>" ></span> (D) <?php echo $option4; ?>
        </label>
    <?php }?><br/><br/>
    

    【讨论】:

      【解决方案2】:

      这可能会帮助您了解概念

              <?php 
      
              if(isset($_POST['answer'])){
                  $option1 =$_POST['answer'];
              }else{
                  $option1 ='undefined';
              }
              $single_answer1 ='c';
      
              ?>
              <?php if($option1 == $single_answer1) {
                  $outer_label_style ="font-size:14px;background-color:green;border:1px solid green;padding:10px";    
                  $inner_i_class ="fa fa-fw fa-check-circle-o";
                  $inner_label_style ="color:green;font-size:15px";
              } else if($option1 <> 'undefined' && $single_answer1!=$option1) {               
                  $outer_label_style ="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px";  
                  $inner_i_class ="fa fa-fw fa-times";
                  $inner_label_style ="color:red;font-size:15px";
              } else if($option1 =='undefined') {         
                  $outer_label_style ="font-size:14px;background-color:yellow;border:1px solid green;padding:10px";   
                  $inner_i_class ="fa fa-fw fa-check-circle-o";
                  $inner_label_style ="color:green;font-size:15px";
               } else { 
                  $outer_label_style ="font-size:14px";   
                  $inner_i_class ="";
                  $inner_label_style ="";
              } ?>
      
              <br/><br/>
      
               <?php 
      
                   $style['a']['outer_label_style'] = "";
                   $style['a']['inner_i_class'] = "";
                   $style['a']['inner_label_style'] = "";   
      
                   $style['b']['outer_label_style'] = "";
                   $style['b']['inner_i_class'] = "";
                   $style['b']['inner_label_style'] = "";
      
                   $style['c']['outer_label_style'] = "";
                   $style['c']['inner_i_class'] = "";
                   $style['c']['inner_label_style'] = "";
      
                   $style['d']['outer_label_style'] = "";
                   $style['d']['inner_i_class'] = "";
                   $style['d']['inner_label_style'] = "";
      
      
      
               if($single_answer1 == 'a'){
                   $style['a']['outer_label_style'] = $outer_label_style;
                   $style['a']['inner_i_class']     = $inner_i_class;
                   $style['a']['inner_label_style'] = $inner_label_style;
      
               }elseif($single_answer1 == 'b'){
                   $style['b']['outer_label_style'] = $outer_label_style;
                   $style['b']['inner_i_class']     = $inner_i_class;
                   $style['b']['inner_label_style'] = $inner_label_style;
               }elseif($single_answer1 == 'c'){
                   $style['c']['outer_label_style'] = $outer_label_style;
                   $style['c']['inner_i_class']     = $inner_i_class;
                   $style['c']['inner_label_style'] = $inner_label_style;
               }elseif($single_answer1 == 'd'){
                   $style['d']['outer_label_style'] = $outer_label_style;
                   $style['d']['inner_i_class']     = $inner_i_class;
                   $style['d']['inner_label_style'] = $inner_label_style;
               }
      
               if($option1 <> 'undefined' && $single_answer1!=$option1){       
      
                   $style[$single_answer1]['outer_label_style'] = "font-size:14px;background-color:lightgray;border:1px solid green;padding:10px";
                   $style[$single_answer1]['inner_i_class']     = "fa fa-fw fa-times";
                   $style[$single_answer1]['inner_label_style'] = "color:red;font-size:15px";
      
                   $style[$option1]['outer_label_style'] = "font-size:14px;background-color:lightgray;border:1px solid red;padding:10px";
                   $style[$option1]['inner_i_class']     = "fa fa-fw fa-check-circle-o";
                   $style[$option1]['inner_label_style'] = "color:green;font-size:15px";
      
                  }
               ?>       
      
      
              <form method='post'>
               <label style="<?php echo $style['a']['outer_label_style'];?>" >
                  <label class="review-icon"><i class="<?php echo $style['a']['inner_i_class'];?>" style="<?php echo $style['a']['inner_label_style'];?>"></i></label>            
                  (A) <input type="radio" name="answer" value="a" <?php echo (isset($_POST['answer']) && $_POST['answer'] =='a')?'checked':'';?> > A) Any student who does not have the power to learn in a group is almost always unsuccessful<br>
              </label>
              <br/><br/>
              <label style="<?php echo $style['b']['outer_label_style'];?>" >
                  <label class="review-icon"><i class="<?php echo $style['b']['inner_i_class'];?>" style="<?php echo $style['b']['inner_label_style'];?>"></i></label>            
                  (<input type="radio" name="answer" value="b" <?php echo (isset($_POST['answer']) && $_POST['answer'] =='b')?'checked':'';?>> (B) EDPeer group pressure and mutual understanding of any concept develops better understanding among students<br>
              </label>
              <br/><br/>
              <label style="<?php echo $style['c']['outer_label_style'];?>" >
                  <label class="review-icon"><i class="<?php echo $style['c']['inner_i_class'];?>" style="<?php echo $style['c']['inner_label_style'];?>"></i></label>            
                  <input type="radio" name="answer" value="c" <?php echo (isset($_POST['answer']) && $_POST['answer'] =='c')?'checked':'';?>> (C) ESAny student who does not have the power to learn in a group is almost always unsuccessful <br>
              </label>
              <br/><br/>
              <label style="<?php echo $style['d']['outer_label_style'];?>" >
                  <label class="review-icon"><i class="<?php echo $style['d']['inner_i_class'];?>" style="<?php echo $style['d']['inner_label_style'];?>"></i></label>            
                  <input type="radio" name="answer" value="d" <?php echo (isset($_POST['answer']) && $_POST['answer'] =='d')?'checked':'';?>> (D) Peer group pressure and mutual understanding of any concept develops better understanding among students <br>
              </label>
              <br/><br/>
               <button type="submit">Submit </button>
              </form>
      

      【讨论】:

        【解决方案3】:
        <?php $rightAnswer = $option1; // get right answer from db?>
        <?php if($rightAnswer == $option1): ?>    
        <?php if ($option1 == $single_answer1) { ?>
        <label style="font-size:14px;background-color:lightgray;border:1px solid green;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
            <span class="">(A) <?php echo $option1; ?>
        </label>
        <?php } else if ($single_answer1 != $option1) {
        ?>
        <label style="font-size:14px;background-color:lightgray;border:1px solid red;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-times" style="color:red;font-size:15px"></i></label>
            <span class="">(A) <?php echo $option1; ?>
        </label>
        <?php } else if ($single_answer1 == 'undefined' || empty($single_answer1)) { ?>
        <label style="font-size:14px;background-color:yellow;border:1px solid green;padding:10px" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
            <span class="">(A) <?php echo $option1; ?>
        </label>
        <?php } ?>
        <?php else: ?>
        <label style="font-size:14px">
            <span class=""><input type="radio" name="single_question_answer1" value="<?php echo $option1; ?>" ></span> (A) <?php echo $option1; ?>
        </label>
        <?php endif; ?><br/><br/>
        

        希望这会有所帮助。 更新: 我创建了一个函数来获取正确的类/css

        <?php
         //function to get option class
         //$rightOption - right answer
         //$currentOption - option name like option1 / option2 / option3 / option4
        function getOptionClass($rightOption, $currentOption, $selectedOption=''){
            if(empty($selectedOption) && $rightOption == $currentOption){
            $optionClass = 'yelloClass'; //write style for this class in your css file 
            }elseif($currentOption == $rightOption && $currentOption == $selectedOption){
            $optionClass = 'greenClass';
            }elseif($currentOption == $selectedOption && $currentOption != $rightOption){
            $optionClass = 'redClass';
            }else{
            $optionClass = ''; // or assign your default class
            }
          return $optionClass;
         }
         ?>
         //for $option1
         $option_class = getOptionClass($rightOption, $option1, $selectedOption); //
         <label style="<?php echo $option_class; ?>" >
             <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
             <span class="">(A) <?php echo $option1; ?>
         </label>
         //for $option2
         $option_class = getOptionClass($rightOption, $option2, $selectedOption); //
         <label style="<?php echo $option_class; ?>" >
            <label class="review-icon"><i class="fa fa-fw fa-check-circle-o" style="color:green;font-size:15px"></i></label>
            <span class="">(A) <?php echo $option1; ?>
         </label>
        

        希望这会对你有所帮助。

        【讨论】:

        • 我们需要与所有其他选项进行比较。这样我们就可以区分选择和正确的答案。未选择和正确的答案。选错答案
        猜你喜欢
        • 1970-01-01
        • 2022-11-13
        • 2018-11-30
        • 2012-12-31
        • 1970-01-01
        • 2021-04-21
        • 1970-01-01
        • 1970-01-01
        • 2022-10-17
        相关资源
        最近更新 更多