我的表单有几个字段,其中三个字段具有从1到5的数字的选择选项。
假设我有以下选择器:
$Cake= intval($_POST["Cake"]);
$Candy= intval($_POST["Candy"]);
$Box= intval($_POST["Box"]);
那么我如何对上述字段值进行计数并返回如下响应:
//Box has 1 selected and Cake has 2 selected
array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
//Box has 2 selected and Cake has 3 selected and Candy has 1 selected
array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
array_push($box,array("paxType" => "Cake"), array("paxType" => "Candy"));
等等。
我知道我需要按如下方式制作计数公式和 if 规则(它不正确,我将其用于显示我需要执行的一些操作)
if ( count($Box) == 2 count($Cake) == 4)
array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
每个盒子最多必须包含 2 个蛋糕和 2 个糖果。例1
- 搜索 : 框 ->2 ;蛋糕 ->4
- 结果:盒(1) ->2个蛋糕;盒子(2) -> 2蛋糕;
例2
- 搜索 : 框 ->2 ;蛋糕 ->4 ;糖果 ->1
- 结果:盒(1) ->2个蛋糕;盒子(2) -> 2个蛋糕和糖果 ->2;
我怎样才能让它工作?非常感谢您的时间,祝您新年快乐。
您需要使用 == 而不是 = 进行比较,并且还需要添加一个 AND像下面一样
if ( (count($Box) == 2) and (count($Cake) == 4) )
您可以使用
此代码实现您的目标,请尝试一下,如果您有任何问题,请告诉我
$Cake_count= intval($_POST["Cake"]);
$Candy_count= intval($_POST["Candy"]);
$Box_count= intval($_POST["Box"]);
$box_array = array();
for($i=0;$i<$Box_count;$i++)
{
for($j=0;$j<$Cake_count;$j++)
{
$box_array[$i][] = array("paxType" => "Cake");
}
for($k=0;$k<$Candy_count;$k++)
{
$box_array[$i][] = array("paxType" => "Candy");
}
}