【发布时间】:2014-09-09 19:31:44
【问题描述】:
好的,我正在生成一个预先填充了数据库中现有值的表单:
查看:
<tr>
<td><input name="set[<?php echo $cur_set['id']; ?>][order]" value="<?php echo $cur_set['wo_order']; ?>">
<input type="hidden" name="set[<?php echo $cur_set['id']; ?>][ex_id]" value="<?php echo $cur_set['ex_id']; ?>"></td>
<td><input name="set[<?php echo $cur_set['id']; ?>][weight]" value="<?php echo $cur_set['weight']; ?>"></td>
<td><input name="set[<?php echo $cur_set['id']; ?>][reps]" value="<?php echo $cur_set['reps']; ?>"></td>
</tr>
示例输出:
<tr>
<td><input name="set[3][order]" value="1">
<input type="hidden" name="set[3][ex_id]" value="1"></td>
<td><input name="set[3][weight]" value="50.00"></td>
<td><input name="set[3][reps]" value="5"></td>
</tr>
所以我有一系列<tr>s,每个都可以有一个唯一的“id”,但都有相同数量的二级索引[order], [ex_id], [weight]和[reps]。
我无法弄清楚两件事:
- 当我不知道 [id] 将是什么时,如何应用 set_value() 语法。
-
如何在表单上设置验证规则。我试过了:
$this->form_validation->set_rules('set[][order]', 'Sets', 'required');
但它似乎不起作用,因为找不到set[][order]...我可以在里面放某种通配符吗?喜欢set[*][order]?
提前致谢。
乔恩
【问题讨论】:
-
你能解释更多吗,尤其是这个:
So I have a range of these <tr>s each can have a unique 'id' but all have the same number of secondary indices [order], [ex_id], [weight] and [reps]和How to apply the set_value() syntax when I don't know what the [id] is going to be -
你可以尝试使用
input name="order[]",修改set_rulesorder。 -
@Amir 我正在从数据库中获取数据,然后将其拆分为每一行的单独
<input>。所以每一行都有一个id,order,weight,ex_id和reps。所以每个<tr>都包含名为set[(id)][(order/ex_id/weight/reps)]的输入,所以我可以修改它们并且它们知道它们属于哪个集合[id]。但是因为 id 是唯一的,所以当我不知道 id 是什么时,我看不到我是如何编写验证规则的。我需要为set[?][order/ex_id/weight/reps/]提供某种包罗万象的信息。希望这是有道理的 -
好的,我想我找到了解决方案:`foreach($data['sets'] as $set_id) { $this->form_validation->set_rules('set['.$set_id['id '].'][order]', '设置顺序', '必需'); }
-
这是我在我看来解决 set_value 的方法:
<input name="set[<?php echo $cur_set['id']; ?>][order]" value="<?php echo set_value('set['.$cur_set['id'].'][order]', $cur_set['wo_order']); ?>">
标签: php codeigniter multidimensional-array