使用PHP区分选择和正确答案

时间:2017-03-03 13:40:29

标签: php css radio-button

将有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 =" ESAny没有学习能力的学生   一个小组几乎总是不成功的。#34;。

$ single_answer1是学生选择的答案。

从以上选择" option1"是正确的答案,但学生选择"选项3"。

我必须区别于 "选项1"背景绿色
"选项3"背景颜色为红色

如果有时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/>

我试过这样但是我得错了输出请任何人帮我解决这个问题。谢谢你。

3 个答案:

答案 0 :(得分:1)

首先改变你的改组逻辑,

Course.createCourse(newCourse, function (err, course) {
        if (err) throw err;
        console.log(course);
    });

这是你的代码,

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

答案 1 :(得分:0)

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

希望这会对你有所帮助。

答案 2 :(得分:0)

这可以帮助您获得概念

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