0

Hopefully someone can help!

I successfully completed over the weekend my quiz which was a 10 question / multiple choice / multiple answer type scenario - however the brief changed this morning -

It turns out that Question 1 for example:

Q How do you make tea
A) Water
B) Teabag
C) Cup
D) Fish Tank

The client would like it that while A B C are the correct answers, there are other correct answers for example: -

A - Is correct + 1 to score
B - Is correct + 1 to score
C - Is Correct + 1 to score
A & B - Is correct  + 1 to score
A & C - Is correct + 1 to score
B & A - Is correct + 1 to score
B & C - Is correct + 1 to score
A & B & C - Is the Jackpot! + 1 to score

Before this 'amendment' I had stored the results into the Database for Question as A,B,C - so everything was working - so I believe I have to 'explode' the original array so they are individual array elements (I was assuming it would be easier to do it this way) - So my array now looks like this:

    Array ( [0] => A [1] => B [2] => C ) 

I tried to do a if nested if statement:

    if($vex['0'] == 'A')
    {
    echo "Yup, it equals A";
    if($vex['1'] == 'B')
    {
    echo "Yup, it equals B";
    } 

But I realised that the array might not always start with [0] => A etc.

Could help me and point me in the right direction?

Would it simpler for me to store the checkboxes as single values rather than an array and just do a check?

4

3 回答 3

1

使用数组并将它们相交以获得如下评分:

$user_choices = array('A', 'C');
$valid_choices = array('A', 'B', 'C');

$common = array_intersect($user_choices, $valid_choices); // A,C
$score = count($common); // 2

同样:

choices A     -> common = A     -> score = 1
choices D     -> common =       -> score = 0
choices A,B,C -> common = A,B,C -> score = 3
于 2013-01-28T17:07:09.623 回答
0

我认为您可以使用 in_array() 函数。

if(in_array('A',$vex))
{
    echo "Yup, it equals A";
    if(in_array('B',$vex))
    {
        echo "Yup, it equals B";
    }
}
于 2013-01-28T17:11:56.453 回答
0

您可以使用array_intersectin_array

例如,如果他们检查了 A 和 B,正确答案是 B 和 C,你可以这样做:

$aAnswers = array('A', 'B');
$aCorrectAnswers = array('B', 'C');
$bSuccess = count(array_intersect($aAnswers, $aCorrectAnswers) || false);
// $bSuccess == true

这会告诉你他们是否至少做对了一项。如果您需要更具体,您可以执行以下操作:

$aComparedAnswers = array();
foreach($aAnswers as $sAnswer){
    if(in_array($sAnswer, $aCorrectAnswers)){
        $aComparedAnswers[] = $sAnswer;
    }
}

然后您可以比较比较和正确的数组。如果他们的数量相同,他们就会得到所有的问题。比较答案的数量将等于正确答案的数量。

于 2013-01-28T17:13:01.833 回答