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?

0

3 Answers 3

1

Use arrays and intersect them to score things like this:

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

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

And similarly:

choices A     -> common = A     -> score = 1
choices D     -> common =       -> score = 0
choices A,B,C -> common = A,B,C -> score = 3
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for the delay in responding, kids hijacked me - This solution is working brilliantly - again, I am sorry, how do I insert the array from the database into $user_choices ... sorry for newb question - Just trying to learn..
I took a bit of a break, thought about it, realised I needed a foreach loop - sorted it, and all fixed and working, I really appreciate your help. thanks again.
0

I think you could use the in_array() function.

if(in_array('A',$vex))
{
    echo "Yup, it equals A";
    if(in_array('B',$vex))
    {
        echo "Yup, it equals B";
    }
}

1 Comment

Thanks Kasun for your suggestion, as above I am going to try Marc B's suggestion!
0

You could use array_intersect or in_array.

For example, if they checked A and B, and correct answers were B and C, you could do:

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

This would tell you if they got at least one right. If you need to get more specific, you can do:

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

Then you can compare the compared and correct arrays. If they have equal count, they got all the questions. The count of compared answers will be equal to the number of correct answers.

1 Comment

thank you for getting back to me and your time, I have decided to try and use Marc B's solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.