开发者

How do I make these inputs relate to each other?

I have a series of checkbox inputs and corresponding text area inputs to allow specification of quantity.

Here's what the two fields look like when the item is static (i.e. only 1):

<input type="checkbox" name="measure[checked][]" value="<?=$item->id?>">
<input type="hidden" name="measure[quantity][]" value="1" />

Here's what the input fields look like for all items that have a specifiable quantity:

<input type="checkbox" name="measure[checked][]" value="<?=$item->id?>"> 
<input class="item_mult" value="0" type="te开发者_JS百科xt" name="measure[quantity][]" />

This would play nicely, if within the array, it didn't output like this, after collecting it with:

$field = $this->input->post('measure',true);

Array ( 

     [quantity] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 25 [7] => 0 [8] => 0 [9] => 0 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 [18] => 1 [19] => 1 [20] => 1 [21] => 1 [22] => 0 [23] => 0 [24] => 0 [25] => 0 [26] => 0 [27] => 0 [28] => 0 [29] => 0 [30] => 0 [31] => 0 [32] => 0 [33] => 0 [34] => 0 [35] => 0 [36] => 0 [37] => 0 [38] => 0 [39] => 0 [40] => 0 [41] => 1 [42] => 1 [43] => 1 [44] => 1 [45] => 1 [46] => 1 [47] => 1 [48] => 1 [49] => 1 [50] => 1 [51] => 1 [52] => 0 [53] => 0 [54] => 0 ) 

     [checked] => Array ( [0] => 4 [1] => 6 [2] => 13 ) 

) 

I understand what the values in the checked array are, I just do not understand how to relate the first field to the second, later in the program.

How do I incorporate the correct code to relate checked items to quantities?


Use something common (the item id) to relate them:

<input type="checkbox" name="measure[checked][<?=$item->id?>]" value="1"> 
<input class="item_mult" value="0" type="text" name="measure[quantity][<?=$item->id?>]" />

Now you'll know what measures relate to what items. However, a better way would be:

<input type="checkbox" name="measure[<?=$item->id?>][checked]" value="1"> 
<input class="item_mult" value="0" type="text" name="measure[<?=$item->id?>][quantity]" />

Here you have an array (measure) of items, with two 'properties': 'checked' and 'quantity'. Easy to iterate over and understand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜