我需要将id值从视图传递给控制器,但我有2个选项
我认为两者都无法使用,如果可能请指导我
<?php for($i=0;$i<count($array['value']);$i++) { ?>
<?php $id= $array['value'][$i]['arrayIndex']; ?>
echo form_open('controller/array_storage/'.$id)
<input type="hidden" name="<?php echo 'foo'.$i ?>" value="<?php echo $array['value'][$i]['foo']?>" />
<input type="hidden" name="<?php echo 'boo'.$i?>" value="<?php echo $array['value'][$i]['boo']?>" />
<input type="hidden" name="<?php echo 'bar'.$i?>" value="<?php echo $array['value'][$i]['bar']?>" />
<input type="submit">
<? } ?>
这是我的控制器
<?php
function array_storage($id)
{
$foo = $this->input->post('foo'.$id);
$boo = $this->input->post('boo'.$id);
$bar = $this->input->post('bar'.$id);
}
?>
这是我的数组
array(4) { ["aa"]=> int(12) ["b"]=> string(4) "2222" ["c"]=> string(3) "232" ["array"]=> array(4) { [0]=> array(7) { ["test"]=> string(5) "23132" ["aaa"]=> int(131) ["bbb"]=> string(4) "sgsg" ["ccc"]=> string(6) "qweqrq" ["ddd"]=> NULL ["eee"]=> int(0) ["value"]=> array(4) { ["foo"]=> int(46546) ["boo"]=> string(6) "231231" ["bar"]=> string(4) "test" ["TDS"]=> string(3) "0.0" } } [1]=> array(7) { ["test"]=> string(5) "23132" ["aaa"]=> int(131) ["bbb"]=> string(4) "sgsg" ["ccc"]=> string(6) "qweqrq" ["ddd"]=> NULL ["eee"]=> int(0) ["value"]=> array(4) { ["foo"]=> int(46546) ["boo"]=> string(6) "231231" ["bar"]=> string(4) "test" ["TDS"]=> string(3) "0.0" } } [2]=> array(7) { ["test"]=> string(5) "23132" ["aaa"]=> int(131) ["bbb"]=> string(4) "sgsg" ["ccc"]=> string(6) "qweqrq" ["ddd"]=> NULL ["eee"]=> int(0) ["value"]=> array(4) { ["foo"]=> int(46546) ["boo"]=> string(6) "231231" ["bar"]=> string(4) "test" ["TDS"]=> string(3) "0.0" } } [3]=> array(7) { ["test"]=> string(5) "23132" ["aaa"]=> int(131) ["bbb"]=> string(4) "sgsg" ["ccc"]=> string(6) "qweqrq" ["ddd"]=> NULL ["eee"]=> int(0) ["value"]=> array(4) { ["foo"]=> int(46546) ["boo"]=> string(6) "231231" ["bar"]=> string(4) "test" ["TDS"]=> string(3) "0.0" } } } }
帮我解决问题
让json轻松查看
{
"aa": 12,
"b": "2222",
"c": "232",
"array": [
{
"arrayIndex": "1",
"aaa": 131,
"bbb": "sgsg",
"ccc": "qweqrq",
"ddd": null,
"eee": 0,
"value": {
"foo": 46546,
"boo": "231231",
"bar": "test",
"TDS": "0.0"
}
},
{
"arrayIndex": "2",
"aaa": 131,
"bbb": "sgsg",
"ccc": "qweqrq",
"ddd": null,
"eee": 0,
"value": {
"foo": 46546,
"boo": "231231",
"bar": "test",
"TDS": "0.0"
}
},
{
"arrayIndex": "3",
"aaa": 131,
"bbb": "sgsg",
"ccc": "qweqrq",
"ddd": null,
"eee": 0,
"value": {
"foo": 46546,
"boo": "231231",
"bar": "test",
"TDS": "0.0"
}
},
{
"arrayIndex": "4",
"aaa": 131,
"bbb": "sgsg",
"ccc": "qweqrq",
"ddd": null,
"eee": 0,
"value": {
"foo": 46546,
"boo": "231231",
"bar": "test",
"TDS": "0.0"
}
}
]
}
答案 0 :(得分:0)
我在本地尝试了以下代码,如果这与您的问题类似,请查看并相应地找到并替换。我已经测试了此代码并且工作正常。
//This is your view
<?php for($i=0;$i<3;$i++) { ?>
<?php $id= '1';
echo form_open('index.php/welcome/array_storage/'.$id)?>
<input type="hidden" name="<?php echo 'foo'.$i ?>" value="<?php echo "2"?> />
<input type="hidden" name="<?php echo 'boo'.$i?>" value="<?php echo "23"?> />
<input type="hidden" name="<?php echo 'bar'.$i?>" value="23" />
<input type="submit">
<?php } ?>
//this is controller function
function array_storage($id)
{
echo $id;
echo $this->input->post('foo'.$id);
echo $this->input->post('boo'.$id);
}
答案 1 :(得分:0)
为什么不试试这个
<?php $total_count = count($array['value']);
for($i=0;$i<total_count ;$i++) { ?>
<?php $id= $array['value'][$i]['arrayIndex']; ?>
// @note the name attribute of the form is set
echo form_open('controller/array_storage/'.$id , "name='form-storage-$id'")
<input type="hidden" name="foo" value="<?php echo $array['value'][$i]['boo']?>" />
<input type="hidden" name="bar" value="<?php echo $array['value'][$i]['bar']?>" />
然后控制器
function array_storage($id = '')
{
$array = get_the_array();
if( array_key_exists( $id , $array )
{
$foo = $this->input->post('foo');
$boo = $this->input->post('boo');
$bar = $this->input->post('bar');
else
{
// error : invalid index
}
}