我想在php中创建一组规则 我有一个包含2个选择字段的表单(值为1> 5
Cookies are:
$Field1 = intval($_POST["Field1"]);
$Field2= intval($_POST["Field2"]);
我想在下面创建一个规则
Field1 has 1 selected and Field2 has 2 selected
if ( count($Field1) = 1) and ( count($Field2) = 2)
array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
或
Field1 has 2 selected and Field2 has 3 selected
if ( count($Field1) = 2) and ( count($Field2) = 4)
array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
或
Field1 has 2 selected and Field2 has 3 selected
if ( count($Field1) = 2) and ( count($Field2) = 3)
array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
array_push($box,array("paxType" => "Cake"));
等等。
这是可能的吗?
上面的代码不正确,但我已输入它以显示我需要的内容。
答案 0 :(得分:0)
您期望得到什么结果?
如果你做这样的事情:
$box = array();
// Field1 has 2 selected and Field2 has 3 selected
if ($Field1 == 2) and ($Field2 == 4) {
array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
array_push($box,array("paxType" => "Cake"), array("paxType" => "Cake"));
}
print_r($box);
唯一的结果是一个数组,其中包含您在$ Field2中提交的项目数。
//output:
Array
(
[0] => Array([paxType] => Cake),
[1] => Array([paxType] => Cake),
[2] => Array([paxType] => Cake),
[3] => Array([paxType] => Cake)
)
答案 1 :(得分:0)
由于您最近的声明:
结果应该是:一个awoat有3个蛋糕,一个有2个 蛋糕。一个阵列可以填充3个蛋糕naxinun。
以下代码将按预期执行:
<?php
class boxes
{
private $_boxes = array();
public $maxCake = 3; // default .. 3
public function addBox()
{
$this->_boxes[] = array();
}
public function getBoxes()
{
return $this->_boxes;
}
public function addCake($boxNum = 0, $cake = array('paxType' => 'Cake'))
{
if (empty($this->_boxes)) {
$this->addBox();
}
if (empty($this->_boxes[$boxNum])) {
$this->_boxes[$boxNum] = array();
}
// if box is full with cakes (maxCake) create a new one
if (count($this->_boxes[$boxNum]) == $this->maxCake) {
$oldBoxNum = $boxNum;
// searching for a box with enough space for another cake
foreach ($this->_boxes as $key => $box) {
if (count($box) != $this->maxCake) {
$boxNum = $key;
break;
}
}
// if we can't found one we add a new box
if ($oldBoxNum == $boxNum) {
$this->addBox();
$boxNum++;
}
}
$this->_boxes[$boxNum][] = $cake;
}
}
$Field1 = 1; // boxes
$Field2 = 5; // cakes
$boxes = new boxes();
$boxes->maxCake = 3; // if there are more than 3 cakes in a box, a new box will be created
// create number of boxes given with field1
for ($x = 0; $x < $Field1; $x++) {
$boxes->addBox();
}
// add cakes given with field2
$y = 0;
for ($x = 0; $x < $Field2; $x++) {
$boxes->addCake($y);
$y++;
if ($Field1 == $y) {
$y = 0;
}
}
$result = $boxes->getBoxes();
for ($x = 0; $x < count($result); $x++) {
echo 'Box' . ($x + 1) . ':';
echo "<br>";
print_r($result[$x]);
echo "<br>";
}
输出:
Box1:
Array (
[0] => Array( [paxType] => Cake )
[1] => Array( [paxType] => Cake )
[2] => Array( [paxType] => Cake )
)
Box2:
Array (
[0] => Array( [paxType] => Cake )
[1] => Array( [paxType] => Cake )
)