如何在其他函数中使用函数变量

时间:2013-05-20 11:54:47

标签: php codeigniter

我有需要使用enterie id的函数。按id选择enterie,显示它并更新DB中的行。 不要复制所有代码,只是主要想法

控制器

function edit_advert()
{
   $id = $this->uri->segment(3); //Here i get my enterie id 
   $this->load->model('model_adverts');
   $myadvert = $this->model_adverts->get_enterie($id); //passing id to model function
 //where i find enterie,  select it from DB and pass it back to controller  
   //and then do if() stuff,  where i give error message or pass returned enterie to
//view and display it .
}

function update_enterie() //in view i got form with values from DB, after editing
//fields, form submits to this function
{ 
   do form validation stuff etc
  And here i need to get $id again and pass it to model, so i can do query update
  where $id = id 
 }

那么,我如何再次访问该$ id变量?

1 个答案:

答案 0 :(得分:-1)

试试这个:

class abc{
    var $id;    
    function edit_advert()
    {
       $this->id = $this->uri->segment(3); //Here i get my enterie id 
       .....
    }
    function update_enterie()
    { 
       //use here $this->id;
    }
}

如果您的update_enterie()未同时被调用,那么您应该使用'session variable' 等,

$this->session->set_userdata('my_id', 'value');

并使用你想要的地方。

了解更多http://ellislab.com/codeigniter/user-guide/libraries/sessions.html