按表单中的类别计算所选复选框(PHP)

时间:2015-05-11 13:58:15

标签: php checkbox count

我的网站上有一个表单,你必须使用复选框检查一些内容。复选框名称根据用户检查此复选框的“点数”而有所不同。为清楚起见,如果复选框为您提供1分,则复选框的所有名称均为name="this_gives_1"name="this_gives_5"给你obv。 5分。现在最后我用以下方法总结所有要点:

$points_in_total= 0;

$question_one_point = $_POST['this_gives_1'];
$question_one_point_count = count($question_one_point);

$points_in_total = $points_in_total + $question_one_point_count;

$question_two_point = $_POST['this_gives_2'];
$question_two_point_count = count($question_two_point)*2;

$points_in_total = $points_in_total + $question_two_point_count;

$question_three_point = $_POST['this_gives_3'];
$question_three_point_count = count($question_three_point)*3;

$points_in_total = $points_in_total + $question_three_point_count;

$question_five_point = $_POST['this_gives_5'];
$question_five_point_count = count($question_five_point)*5;

$points_in_total = $points_in_total + $question_five_point_count;

$question_eight_point = $_POST['this_gives_8'];
$question_eight_point_count = count($question_eight_point)*8;

$points_in_total = ceil(($points_in_total);
$email_message .= "You got ".$points_in_total";

现在我想要做的是,我想添加一个系统来检查一些事情然后使用简单的布尔告诉,如果这个用户是或不是。

例如,如果存在“Porche”,“法拉利”,“菲亚特”以及用户甚至其中之一的复选框,则系统将认为:类别“汽车”中的已选中复选框的数量> 0,所以这个用户喜欢汽车。如果数字是例如0,那么它说:“我认为你不喜欢汽车”。

所以我可以通过以下方式将ID用于所有与汽车相关的导体:

<input id="car" name="this_gives_5[]" type="checkbox" value="Ferrari">Ferrari
<input id="car" name="this_gives_1[]" type="checkbox" value="Fiat">Ferrari
<input id="fruit" name="this_gives_1[]" type="checkbox" value="Banana">Banana
<input id="fruit" name="this_gives_3[]" type="checkbox" value="Apple">Apple
<input id="fruit" name="this_gives_5[]" type="checkbox" value="Tomato">Tomato

然后使用ID来计算,检查了多少“汽车”(或水果等)?

if ($points_in_total_of_ID_cars > 2) {
    $email_message .= "You really like cars!";
    } elseif ($lukumaara > 0) {
    $email_message .= "You like cars";
    } else {
    $email_message .= "You dont like cars, because you didn't check one";
    }

再一次,我该如何

1)建立这个,我可以算一个特定类别的项目; 2)创建简单的if-elseif以向用户提供上述示例中的反馈?

0 个答案:

没有答案