使用php可以通过正确答案区分选择



将有4个选项Option1,option2,option3,option4

(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 ="没有能力学习的Esany学生 一个小组几乎总是失败的"。

$ single_answer1是学生选择的答案。

从上面的选择中" option1"是正确的答案,但学生选择" option3"。

我必须像
一样区分背景绿色的"选项1"
背景颜色红色

的" option3"

如果有时option1并回答两个相等,则给出边框颜色绿色。

如果Single_answer为"未定义"或空值,则它将用黄色背景显示正确的答案(未选择Anythng&正确答案)。

<?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/>

我尝试过这样的尝试,但我遇到了错误的输出,请任何人帮助我摆脱这个问题。谢谢。

首先更改您的洗牌逻辑,

$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/>

这可能有助于您获得概念

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

希望这对您有帮助。

最新更新