用PHP进行多重答案的多选择辨证选择

时间:2017-03-23 12:33:03

标签: php css arrays checkbox comparison

我有多个选择有多个答案。我想要不同的选项,正确的答案和错误的答案。我已经用单一答案完成了这类问题,但在这种情况下,正确的答案可能不止一个。 Differentiate Choices with Correct Answer using PHP。我曾经尝试过以下代码,但我不会为所有人进行单一价值检查。我不知道为什么

<?php 
$a1 = [trim($m_o1), trim($m_o2), trim($m_o3), trim($m_o4), trim($m_o5)];
shuffle($a1);
$b1      = ['m_o1', 'm_o2', 'm_o3', 'm_o4' ,'m_o5'];
$result1 = array_combine($b1, $a1);
extract($result1);
$sc_flag2=0;
$actual_flag2=0;
if($m_c1=='1')
{
    $correct_answer2 = array(
        $m_oo1
    );

}
else if($m_c1=='2')
{
    $correct_answer2 = array(
        $m_oo1,
        $m_oo2
    );

}
else if($m_c1=='3')
{
    $correct_answer2 = array(
        $m_oo1,
        $m_oo2,
        $m_oo3
    );

}
else if($m_c1=='4')
{
    $correct_answer2 = array(
        $m_oo1,
        $m_oo2,
        $m_oo3,
        $m_oo4
    );

}
else if($m_c1=='5')
{
$correct_answer2 = array(
        $m_oo1,
        $m_oo2,
        $m_oo3,
        $m_oo4,
        $m_oo5
    );

}
else
{
    $correct_answer2 = array();

}

$multiple_answer = array(
        $multiple_answer_1_1,
        $multiple_answer_1_2,
        $multiple_answer_1_3,
        $multiple_answer_1_4,
        $multiple_answer_1_5
    );
//print_r($multiple_answer);
//print_r($correct_answer2);

if (in_array($m_o1, $correct_answer2) && in_array($multiple_answer, $correct_answer2)) {
    $sc_flag2 = 1; // student's selected correct flag
}
if (in_array($m_o2, $correct_answer2) && in_array($multiple_answer, $correct_answer2)) {
    $sc_flag2 = 2; // student's selected correct flag
}
if (in_array($m_o3, $correct_answer2) && in_array($multiple_answer, $correct_answer2)) {
    $sc_flag2 = 3; // student's selected correct flag
}
if (in_array($m_o4, $correct_answer2) && in_array($multiple_answer, $correct_answer2)) {
    $sc_flag2 = 4; // student's selected correct flag
}
if (in_array($m_o5, $correct_answer2) && in_array($multiple_answer, $correct_answer2)) {
    $sc_flag2 = 5; // student's selected correct flag
}

if (in_array($m_o1, $correct_answer2)) {
    $correct_flag2 = 1; // student's selected correct flag
}
if (in_array($m_o2, $correct_answer2)) {
    $correct_flag2 = 2; // student's selected correct flag
}
if (in_array($m_o3, $correct_answer2)) {
    $correct_flag = 3; // student's selected correct flag
}
if (in_array($m_o4, $correct_answer2)) {
    $correct_flag2 = 4; // student's selected correct flag
}
if (in_array($m_o5, $correct_answer2)) {
    $correct_flag2 = 5; // student's selected correct flag
}
if (in_array($m_o1, $multiple_answer)) {
    $actual_flag2 = 1; // student's selected correct flag
}
if (in_array($m_o2, $multiple_answer)) {
    $actual_flag2 = 2; // student's selected correct flag
}
if (in_array($m_o3, $multiple_answer)) {
    $actual_flag2 = 3; // student's selected correct flag
}
if (in_array($m_o4, $multiple_answer)) {
    $actual_flag2 = 4; // student's selected correct flag
}
if (in_array($m_o5, $multiple_answer)) {
    $actual_flag2 = 5; // student's selected correct flag
}
?>
<?php if ($sc_flag2 == 1) { // correct answer by student green// see for correct option I had added green border and bg but tht time alps yello bg oisk showingokok ?>
    <label style="font-size:14px;background-color:#90EE90;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 $m_o1; ?>
    </label>
<?php } else if ($actual_flag2 == 1 && $correct_flag2 != 1) { // student's answer is wrong here and student answer is given red
    ?>
    <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 $m_o1; ?>
    </label>
<?php } else if ($correct_flag2 == 1) { // student have not given answer and correct option will be yellow ?>
    <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:green;font-size:15px"></i></label>
        <span class="">(A) <?php echo $m_o1; ?>
    </label>
<?php } else {?>
    <label style="font-size:14px">
        <span class=""><input type="checkbox" value="<?php echo $m_o1; ?>" ></span> (A) <?php echo $m_o1; ?>
    </label>
<?php }?><br/><br/>
<?php if ($sc_flag2 == 2) {?>
    <label style="font-size:14px;background-color:#90EE90;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 $m_o2; ?>
    </label>
<?php } else if ($actual_flag2 == 2 && $correct_flag2 != 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 $m_o2; ?>
    </label>
<?php } else if ($correct_flag2 == 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:green;font-size:15px"></i></label>
        <span class="">(B) <?php echo $m_o2; ?>
    </label>
<?php } else {?>
    <label style="font-size:14px">
        <span class=""><input type="checkbox" value="<?php echo $m_o2; ?>" ></span> (B) <?php echo $m_o2; ?>
    </label>
<?php }?><br/><br/>
<?php if ($sc_flag2 == 3) {?>
    <label style="font-size:14px;background-color:#90EE90;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 $m_o3; ?>
    </label>
<?php } else if ($actual_flag2 == 3 && $correct_flag2 != 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 $m_o3; ?>
    </label>
<?php } else if ($correct_flag2 == 3) {?>
    <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:green;font-size:15px"></i></label>
        <span class="">(C) <?php echo $m_o3; ?>
    </label>
<?php } else {?>
    <label style="font-size:14px">
        <span class=""><input type="checkbox" value="<?php echo $m_o3; ?>" ></span> (C) <?php echo $m_o3; ?>
    </label>
<?php }?><br/><br/>
<?php if ($sc_flag2 == 4) {?>
    <label style="font-size:14px;background-color:#90EE90;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 $m_o4; ?>
    </label>
<?php } else if ($actual_flag2 == 4 && $correct_flag2 != 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 $m_o4; ?>
    </label>
<?php } else if ($correct_flag2 == 4) {?>
    <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:green;font-size:15px"></i></label>
        <span class="">(D) <?php echo $m_o4; ?>
    </label>
<?php } else {?>
    <label style="font-size:14px">
        <span class=""><input type="checkbox" value="<?php echo $m_o4; ?>" ></span> (D) <?php echo $m_o4; ?>
    </label>
<?php }?><br/><br/>
<?php if ($sc_flag2 == 5) {?>
    <label style="font-size:14px;background-color:#90EE90;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="">(E) <?php echo $m_o5; ?>
    </label>
<?php } else if ($actual_flag2 == 5 && $correct_flag2 != 5) {
    ?>
    <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="">(E) <?php echo $m_o5; ?>
    </label>
<?php } else if ($correct_flag2 == 5) {?>
    <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:green;font-size:15px"></i></label>
        <span class="">(E) <?php echo $m_o5; ?>
    </label>
<?php } else {?>
    <label style="font-size:14px">
        <span class=""><input type="checkbox" value="<?php echo $m_o5; ?>" ></span> (D) <?php echo $m_o5; ?>
    </label>
<?php }?><br/><br/>

我的数组值是

 for $a1=which means list of options like

        Array ( [0] => Larger animals in the same territory may vacate their burrows for meerkats to stay in them. [1] => The organised lifestyle and the peculiar body structures help meerkats live a comfortable life in extremities. [2] => Territorial fights are usually organised to solve disputes about all problems. [3] => Meerkats show strong solidarity with their group members for the sake of their safety and comfort. [4] => Meerkats dig their burrows in a unique way, having around 15 entrance and exit holes. )

学生给出的答案我们需要比较这些选项

$ multiple_answers =数组([0] =&gt;同一地区的较大动物可能会腾出洞穴让猫鼬留在其中。[1] =&gt;猫鼬以独特的方式挖掘洞穴,大约有15个入口和退出孔。[2] =&gt; undefined [3] =&gt; undefined [4] =&gt; undefined)

我正确的answer2=Array ( [0] => Meerkats show strong solidarity with their group members for the sake of their safety and comfort. [1] => The organised lifestyle and the peculiar body structures help meerkats live a comfortable life in extremities. )

这里我希望结果为

[0] =&gt;在同一地区的较大的动物可能会腾出洞穴让猫鼬留在它们中。 [1] =&gt;猫鼬以独特的方式挖掘他们的洞穴,有大约15个入口和出口孔。这两个选项值为红色。和[0] =&gt;的选项为了安全和舒适,猫鼬与团队成员表现出强烈的团结。 [1] =&gt;有组织的生活方式和独特的身体结构帮助猫鼬在四肢中过着舒适的生活。用黄色。但我得到输出错误的答案。请检查我的代码,让我知道什么是错误?很乐意帮助我摆脱这个问题。谢谢。谢谢你。感谢你。

1 个答案:

答案 0 :(得分:2)

您必须将标志声明为数组,并将选择推送到该数组,这样,如果有多个选项,则不会替换存储的值。在旗帜中。

{{1}}