0

i have been spending the entire weekend on this but cannot seem to get it solved.

My html consists of two drop down menus. One determines a specific type and the second determines a component.

There is predefined costs associated with each component based on the type its being used for.

Here my HTML:

 <td><select name="type">
<option value= "0" selected>None</option>
<option value = "CAR">Car</option>
<option value = "VAN">Van</option>
<option value = "TRK">Truck</option>
</select></td>

<td>Component:</td>
<td><select name="component">
<option value= "0" selected>None</option>
<option value = "TIR">Oil</option>
<option value = "OIL">Tire</option>
<option value = "SPK">Spark Plug</option>
</select></td>

My php looks like this:

$cat = array(
'CAR_TIR' => array('comp' => 'Tires','price' => 100),
'CAR_OIL' => array('comp' => 'Oil', 'price' => 10),
'CAR_SPK' => array('comp' => 'Spark Plug', 'price' => 4),

'VAN_TIR' => array('comp' => 'Tires','price' => 120),
 'VAN_OIL' => array('comp' => 'Oil', 'price' =>12),
      'VAN_SPK' => array('comp' => 'Spark Plug', 'price' =>5),

'TRK_TIR' => array('comp' => 'Tires','price' => 150),
'TRK_OIL' => array('comp' => 'OIL','price' => 15),
'TRK_SPK' => array('comp' => 'Tires','price' => 6),

);
echo $cat[$_POST['type'].'_'.$_POST['component']]['price']; 
?>

and the output that i am getting looks like this : Price checkup? array('comp' => 'Tires','price' => 100), 'CAR_OIL' => array('comp' => 'Oil', 'price' => 10), 'CAR_SPK' => array('comp' => 'Spark Plug', 'price' => 4), 'VAN_TIR' => array('comp' => 'Tires','price' => 120), 'VAN_OIL' => array('comp' => 'Oil', 'price' =>12), 'VAN_SPK' => array('comp' => 'Spark Plug', 'price' =>5), 'TRK_TIR' => array('comp' => 'Tires','price' => 150), 'TRK_OIL' => array('comp' => 'OIL','price' => 15), 'TRK_SPK' => array('comp' => 'Tires','price' => 6), ); echo $cat[$POST['type'].''.$_POST['component']]['price']; ?>

so literally that is the output. please let me know what i could do. thanks!

4

3 回答 3

0

Is a three dimensional array necessary? It seems like a very messy and hard to read way of doing things to me. Three arrays would be easier to read I would think, if possible?

Write three separate arrays to show the dropdowns (try foreach instead of a for loop) then use the values of each to calculate the price after it has been submitted.

If this is not what you're looking for then you'll have to clarify your questions a little better. :)

于 2013-02-19T01:42:23.930 回答
0

为什么不通过二维和一个关联数组,我发现它更容易阅读:

<td>Car type:</td>
<td><select name="type">
    <option value= "0" selected>None</option>
    <option value = "CAR">Car</option>
    <option value = "VAN">Van</option>
    <option value = "TRK">Truck</option>
</select></td>

<td>Component:</td>
<td><select name="component">
    <option value= "0" selected>None</option>
    <option value = "TIR">Oil</option>
    <option value = "OIL">Tire</option>
    <option value = "SPK">Spark Plug</option>
</select></td>

$cat = array(
    'CAR_TIR' => array('comp' => 'Tires','price' => 100),
    'CAR_OIL' => array('comp' => 'Oil', 'price' => 10),
    'CAR_SPK' => array('comp' => 'Spark Plug', 'price' => 4),
    'VAN_TIR' => array('comp' => 'Tires','price' => 200),
    'TRK_TIR' => array('comp' => 'Tires','price' => 300)
    ); // fill in the missing values for VAN and TRK

参考:

echo $cat['CAR_TIR']['comp']; // output: Tires
echo $cat['CAR_TIR']['price']; // output: 100
echo $cat['TRK_TIR']['price']; // output: 300

引用来自表单的输入(假设typecomponent设置):

echo $cat[$_POST['type'].'_'.$_POST['component']]['price']; 
// type='CAR', component='OIL', Output: 10

循环:

foreach ($cat as $key => $value) {
    echo 'Code: '.$key.', component: '.$value['comp'].', price:'.$value['price'];
}
于 2013-02-19T02:03:23.030 回答
0

您应该使用嵌套关联数组而不是数字。关联数组是其元素由字符串而不是数字引用的数组。这应该使引用每个元素变得更加容易。

对于您的情况,您应该使用以下内容:

$categories = array( 
    'car' => array( 'oil' => 10, 'spark' => 10 ),
    'van' => array( 'oil' => 12, 'spark' => 12 ),
    'truck' => array( 'oil'=> 15, 'spark' => 15 )
);

要从表单中获取所选项目,您可以这样做:

$type = $_POST['type'];
$component = $_POST['component'];
// Error check $type and $component
$selected = $categories[$type][$component];

不要忘记将您的 htmloption值从整数更改为与数组键匹配的字符串。您还需要将 for 循环更改为foreach 循环。

于 2013-02-19T01:47:44.867 回答