以序列化形式存储多个选择框值和相同的输入字段值

时间:2013-05-03 05:54:48

标签: php mysql

<!-- html code -->
<form id="wpsc-settings-form" action="" method="post">
<tr class="rate_row">
<td> 
    <input type="text" style="width:160px;" value="3000" name="zipcode[]">
</td>
<td>
    <select multiple="" name="city[]">
        <option value="Abbotsford">Abbotsford</option>
        <option value="Aberfeldie">Aberfeldie</option>
        <option value="Airport West">Airport West</option>
    </select>
</td>
 <td> 
   <input type="text" style="width:160px;" value="3000" name="zipcode[]">
</td>
<td>
    <select multiple="" name="city[]">
        <option value="Abbotsford">Abbotsford</option>
        <option value="Aberfeldie">Aberfeldie</option>
        <option value="Airport West">Airport West</option>
    </select>
</td>
</tr>    
</form>

enter image description here

<!-- php code -->
<?php
        $options = $_POST['zipcode'];
        $city = $_POST['city'];  
        $serializeoption = serialize($city);

        foreach($city as $key => $value){
            $simple_shipping_options_city[$options[$key]] = $value;
        }

        echo "<pre>";
        print_r($simple_shipping_options_city);
        echo "</pre>";

        update_option('wpsc_simple_shipping_city', $simple_shipping_options_city); // update query
?>  

上面的代码按以下方式工作。如果我选择一个城市输出是这样的:

 Array
(
[3000] => Abbotsford
[2000] => Airport West
)  

如果我选择多个城市而不传递输入字段值。

我希望在多个城市保存单个邮政编码。您能否提供改进上述代码的建议?

1 个答案:

答案 0 :(得分:0)

首先,在这一行

print_r($simple_shipping_options_city);

simple_shipping_options_city是一个数组。您需要告诉数组您想要查看哪个索引。