-2

我有需要使用 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 变量?

4

1 回答 1

-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()没有同时被调用,那么您应该使用“会话变量”,例如,

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

并在您想要的地方使用。

阅读更多http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

于 2013-05-20T12:03:18.257 回答