-3

我想在 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)

4

2 回答 2

1

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

首先创建一个答案数组 $answerArray = array('a','b','c','d')

其次,将用户的响应填充到一个数组中(称为 $userAnswers)

然后,使用类似的东西比较数组

$numCorrect = 0;
for($a; $a < count($answerArray); $a++){
    if($answerArray[$a] == $userAnswers[$a]){
        $numCorrect++;
    }
}
于 2012-08-17T22:15:58.410 回答
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
于 2012-08-17T21:43:34.387 回答