【问题标题】:Codeigniter set_value() With Dynamic Multidimensional Array Inputs具有动态多维数组输入的 Codeigniter set_value()
【发布时间】: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>

所以我有一系列&lt;tr&gt;s,每个都可以有一个唯一的“id”,但都有相同数量的二级索引[order], [ex_id], [weight][reps]

我无法弄清楚两件事:

  1. 当我不知道 [id] 将是什么时,如何应用 set_value() 语法。
  2. 如何在表单上设置验证规则。我试过了:

    $this->form_validation->set_rules('set[][order]', 'Sets', 'required');
    

但它似乎不起作用,因为找不到set[][order]...我可以在里面放某种通配符吗?喜欢set[*][order]

提前致谢。

乔恩

【问题讨论】:

  • 你能解释更多吗,尤其是这个:So I have a range of these &lt;tr&gt;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_rules order
  • @Amir 我正在从数据库中获取数据,然后将其拆分为每一行的单独 &lt;input&gt;。所以每一行都有一个id,order,weight,ex_idreps。所以每个&lt;tr&gt; 都包含名为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 的方法:&lt;input name="set[&lt;?php echo $cur_set['id']; ?&gt;][order]" value="&lt;?php echo set_value('set['.$cur_set['id'].'][order]', $cur_set['wo_order']); ?&gt;"&gt;

标签: php codeigniter multidimensional-array


【解决方案1】:

在控制器中,您获取数据并将其存储在一个变量中(我称之为 $data),执行以下操作:

$data = $this->MODEL_NAME->METHOD_NAME();

foreach($data as $set){
    $this->form_validation->set_rules("set[{$set['id']}][order]");
    $this->form_validation->set_rules("set[{$set['id']}][weight]");
    etc...
}

【讨论】:

    猜你喜欢
    • 2014-10-15
    • 2021-03-07
    • 2012-05-17
    • 2021-09-19
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2011-01-14
    • 2012-01-02
    相关资源
    最近更新 更多