【问题标题】:How to pass a id value from view to controller in codeigniter如何在codeigniter中将id值从视图传递到控制器
【发布时间】:2016-05-07 12:55:40
【问题描述】:

我需要将 id 值从视图传递到控制器,但我有 2 个选项

  1. 使用带有 uri 段的锚标记 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"
      }
    }
  ]
}

【问题讨论】:

  • 尽可能发布您的完整数组...
  • 这是我的示例数组的样子
  • 您似乎忘记在表单操作&lt;?php echo form_open("controller/array_storage/$id"); ?&gt; 中输入$id。 (您在这里拼错了 controller 字)。
  • 我也试过,但 $id 总是返回值 1
  • echo form_open('controller/array_storage/'.$id)

标签: php codeigniter


【解决方案1】:

为什么不试试这个

<?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 
     }
}

【讨论】:

  • name属性有什么用
  • 因为存在多个表单,输入元素具有相同的名称属性。
  • get_the_array();如何得到它
  • get_the_array();应该替换为返回存储在存储中的数据的函数。我只是将其用作 get_the_array() 演示
【解决方案2】:

我在本地尝试了以下代码,如果这与您的问题相似,请查看它,然后相应地查找并替换。我已经测试了此代码,它工作正常。

//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);
 }

【讨论】:

  • 如何在控制器中获取 foo 值
  • @mvd 如果这解决了您的问题,请投票给答案...否则通知我
  • 我提交表单时只返回空值
  • 您是否将 $id 值作为 0、1、2 发送??
  • 通过检查 print_r($this->input->post()) 的值并检查是否设置了任何值来更好地调试。接下来你要做的就是复制我提供的代码并检查这段代码,如果它有效的话。
猜你喜欢
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
相关资源
最近更新 更多