简体   繁体   English

如何将ID值从视图传递到Codeigniter中的控制器

[英]How to pass a id value from view to controller in codeigniter

I need to pass a id value from view to controller but i have 2 options 我需要将ID值从视图传递到控制器,但我有2个选项

  1. using anchor tag with uri segment 2.with hidden field form 使用带有uri分段2的锚标记和隐藏字段形式

I think both cant be used , if possible please guide me 我认为两者都不能使用,如果可以的话请指导我

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

here is my controller 这是我的控制器

  <?php 
   function array_storage($id) 

  {
 $foo = $this->input->post('foo'.$id);
 $boo =   $this->input->post('boo'.$id);
 $bar =  $this->input->post('bar'.$id);
 }
 ?>

here is my array 这是我的数组

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

Help me to fix the issue 帮我解决问题

convreted json to view easily 秘密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"
      }
    }
  ]
}

I tried the following code in my local please see at it if this is similar to your problem then find and replace accordingly.I have tested this code and it works fine. 我在本地尝试了以下代码,如果与您的问题相似,请查看该代码,然后进行查找和替换。我已经测试了此代码,并且工作正常。

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

why not try this 为什么不试试这个

<?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']?>" />

Then controller 然后控制器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM