duan0821
2016-06-27 13:32
浏览 71
已采纳

在codeigniter中将变量从控制器传递到模型时获取Null值

I am having some problem. I am trying to pass a variable from a controller to a model, but I always get a NULL value ON THE MODEL. Anyone can point out what is wrong with my code?

CONTROLLER

public function test()
{
    $this->load->model('model_register');

    //ID Creation to be inserted into the database
    $country_prefix = $this->model_register->countryprefix();
    $org_prefix = $this->model_register->organizationprefix();
    $insertion = $this->model_register->insertion();

    $userid_base = $country_prefix['0']['c_code'].$org_prefix['0']['id'];

    $basenum = "000000000";
    $base_length = strlen($basenum);
    $id_length = strlen($org_prefix['0']['id']);

    $computation = $base_length - $id_length;

    $id_base_array = array();

    for ($i = 0; $i < $computation; $i++)
    {
        array_push($id_base_array, "0");
    }

    $id_base = implode("",$id_base_array);

    $userid_admin = $country_prefix['0']['c_code']."ADM".$id_base.$org_prefix['0']['id'];

    //Insert Data

    //data-mix
    $data['userid'] = $userid_base;
    $data['baselength'] = strlen($basenum);
    $data['id_length'] = strlen($org_prefix['0']['id']);
    $data['result'] = $computation;
    $data['id_base'] = implode("",$id_base_array);
    $data['userid_admin'] = $userid_admin;
    $data['insertion'] = $insertion;

    $insertion = $this->model_register->insertion($data['userid_admin']);

}

Model

public function insertion($x)
{
    $value = $x;

    $data = array(
        'testid' => $value,
    );

    $this->db->insert('test',$data);

    return $value;
}

When I run this, I get

INSERT INTO `test` (`testid`) VALUES (NULL)

图片转代码服务由CSDN问答提供 功能建议

我遇到了一些问题。 我试图将变量从控制器传递给模型,但我总是在模型上得到一个NULL值。 任何人都可以指出我的代码有什么问题吗?

CONTROLLER

  public function test()
 {
 $ this-  &gt; load-&gt; model('model_register'); 
 
 // ID创建要插入数据库
 $ country_prefix = $ this-&gt; model_register-&gt; countryprefix(); 
 $ org_prefix =  $ this-&gt; model_register-&gt; organizationprefix(); 
 $ insertion = $ this-&gt; model_register-&gt; insertion(); 
 
 $ userid_base = $ country_prefix ['0'] ['c_code']  。$ org_prefix ['0'] ['id']; 
 
 $ basenum =“000000000”; 
 $ base_length = strlen($ basenum); 
 $ id_length = strlen($ org_prefix ['0']  ['id']); 
 
 $ computation = $ base_length  -  $ id_length; 
 
 $ id_base_array = array(); 
 
 for($ i = 0; $ i&lt; $ computation; $  i ++)
 {
 array_push($ id_base_array,“0”); 
} 
 
 $ id_base = implode(“”,$ id_base_array); 
 
 $ userid_admin = $ country_prefix ['0']  ['c_code']。“ADM”。$ id_base。$ org_prefix ['0'] ['id']; 
 
 //插入数据
 
 // data-mix 
 $ data ['userid  '] = $  userid_base; 
 $ data ['baselength'] = strlen($ basenum); 
 $ data ['id_length'] = strlen($ org_prefix ['0'] ['id']); 
 $ data ['  result'] = $ computation; 
 $ data ['id_base'] = implode(“”,$ id_base_array); 
 $ data ['userid_admin'] = $ userid_admin; 
 $ data ['insertion'] = $ 插入; 
 
 $ insertion = $ this-&gt; model_register-&gt;插入($ data ['userid_admin']); 
 
} 
   
 
 

模型

 公共函数插入($ x)
 {
 $ value = $ x; 
 
 $ data = array(
'testid'=&gt;  ;  $ value,
); 
 
 $ this-&gt; db-&gt; insert('test',$ data); 
 
返回$ value; 
} 
   
 
 

当我运行它时,我得到

  INSERT INTO`test`(`testid`)VALUES(NULL)
  <  / pre> 
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjin9035 2016-06-28 01:27
    已采纳

    You called insertion twice with two different 'arguments'. The first one $insertion = $this->model_register->insertion(); was called without parameters and the second was called with parameters, $insertion = $this->model_register->insertion($data['userid_admin']);

    This condition may fall under Method Overloading, a condition where the same method is created but with different parameters and as such can perform two different functionalities.

    If you want to use the same function in both cases, however the best fix is to pass a default NULL value as the parameter in the Model file, like this

    public function insertion($x = NULL) {
        // Function code appears here
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题