PHP中的个性测试逻辑

时间:2012-08-17 21:36:20

标签: php

我想在PHP中进行“个性”测试。我想用以下逻辑制作它:

If $answer_one == "a" --> $personality_a = $personality_a+1;
If $answer_one == "b" --> $personality_b = $personality_b+1;
If $answer_one == "c" --> $personality_c = $personality_c+1;
If $answer_one == "d" --> $personality_d = $personality_d+1;

See which $personality_* is the highest one and then send the response.

如何使用foreach($_POST as $k => $v)制作类似的内容,但可能更通用?

2 个答案:

答案 0 :(得分:1)

假设您正在制作表单,并且对于从“查看源”黑客隐藏测试不感兴趣

在html中,与

一致
<input type="radio" name="answer[1]" value="a">
代码中的

<?php

$answers = array_count_values($_POST['answer']);

arsort($answers, SORT_NUMERIC);
reset($answers);
$personalityType = key($answers);

// TODO: validate type is valid

答案 1 :(得分:1)

我的朋友在静态代码上很容易。您可以在这里考虑一个多维数组

首先创建一个答案数组     $ answerArray = array(&#39; a&#39;,&#39;&#39;,&#39; c&#39;&#39; d&#39;)

其次,在数组中填充用户的响应(称为$ userAnswers)

然后,使用

之类的东西比较数组
$numCorrect = 0;
for($a; $a < count($answerArray); $a++){
    if($answerArray[$a] == $userAnswers[$a]){
        $numCorrect++;
    }
}