PHP $ _post为每个元素选择+输入

时间:2015-07-22 07:46:38

标签: php

我需要将每个值(id)链接到一个用于进行计算的量,但我无法弄清楚这是如何工作的。这是我的代码:

HTML

<select name="type[]">
<option value="id">...</option>
</select>
<input name="amount"/>

//This repeats with a loop 8 times. There are 8 select boxes with the same options.

PHP

$type = $_POST['type'];
$id = $_POST['id'];
$amount = $_POST['amount'];

foreach ($products as $thisProduct) {

    foreach ($id as $value) {
        if ($thisProduct->getId() == $value) {
            $multiply = ($thisProduct->getMultiply($amount));
            array_push($array, $multiply);  
        }

        switch($type){
            case "one":
            $multiplyType = ($thisProduct->getMultiplyType($amount));
            array_push($arrayType, $multiplyType);
            break;

            case "two":
            $multiplyType = ($thisProduct->getMultiplyType($amount));
            array_push($arrayType, $multiplyType);
            break;

            case "three":
            $multiplyType = ($thisProduct->getMultiplyType($amount));
            array_push($arrayType, $multiplyType);
            break;
        }
    }
}

这样可行,但值与上一个<input>相乘。因此,当上一个input值= 10时,所有值都会乘以10.将每个input与特定select元素相关联的最简单方法是什么?

1 个答案:

答案 0 :(得分:0)

将文本输入也转换为数组。请查看此答案以获取更多信息:put input type text in an array

相关问题