开发者

PHP Quiz (getting user answers and comparing arrays)

开发者 https://www.devze.com 2023-03-24 06:58 出处:网络
I am a bit new to php and I need to create a quiz website. I have been searching a lot but I couldn\'t find the exact code that I need.

I am a bit new to php and I need to create a quiz website. I have been searching a lot but I couldn't find the exact code that I need.

On the first page, the user will see questions, answers and he can choose answers by clicking radio buttons. Once submitted, I want to compare on the next page 2 arrays of answers (user answers and the correct answers).

The cor开发者_运维知识库rect answers array may look like this: $res1 = array(b,a,b,c,b,d,a,b,c,b); I believe I should use "for" or "for each" but I don't know how to write it.

The next step will be incrementing the quiz score in case the compared values are equal (user choice: b and correct answer: b)


Here is a simple solution for you:

First, create a form like this:

<form>
<input type="radio" name="question_1" value="a" />
<input type="radio" name="question_1" value="b" />
<input type="radio" name="question_1" value="c" />

<input type="radio" name="question_2" value="a" />
<input type="radio" name="question_2" value="b" />
<input type="radio" name="question_2" value="c" />
...
</form>

In php, do something like this:

$answers = array('a', 'b', 'c', 'a', ...);
$points  = 0;

for($i=0; $i < count($answers); $i++) {
    if (isset($_REQUEST['question_'.($i+1)]) && $_REQUEST['question_'.($i+1)] == $answers[$i]) 
        $points++;
}

echo $points;
0

精彩评论

暂无评论...
验证码 换一张
取 消