我不知道你用什么样的后端,而是一种更好的方式将数据存储在数据库中是不是多种类型的数据在同一领域结合起来,并转而使用数字标识的文本的的基于身份的ID。你可以创建一个表如:
id name price
----------------------------------
1 Item 19.99
2 Item 1 0
3 Item 2 24.99
4 Item 3 0
5 Item 4 0
请注意,我的示例显示“0”没有输入价格。这是因为您可能希望将价格列存储为十进制数(小数点后两位),这会使空值默认为“0”。没关系。您的用户界面不必显示“0”。
然后,当你从数据库中检索数据,你可能最终得到一个这样的数组:
foreach ($data as $row) {
explode($row);
if ($price > 0) {
$checked = 'checked="checked"';
} else {
$checked = '';
$price = '';
}
$pattern = ' '
. '%s ';
echo sprintf($pattern, $id, $checked, $name, $id, $price);
}
?>:
Array(
[0] => Array
(
[id] => 1
[name] => Item
[price] => 19.99
)
[1] => Array
[id] => 2
[name] => Item 1
[price] => 0
)
)
and so on...
,那么你可以在你的.PHP像下面显示的值
然后当检索表单输入时,你想要的变量是$_POST['categories']和$_POST['prices']。