Codeigniter - using set_value() to populate a form

2019-03-30 01:34发布

问题:

I have a view containing a form. with input fields and radio buttons. This form needs to be populated with the data from database, so as to use it as an data editing form.

I have used the set_value() function for the form validation where the view is called from the same controller after the post. But how to use set_value function with out a POST? i.e. simply populate the values in a form in the view that's loaded. with say, an Array or object from database.

回答1:

You can use it like this

set_value('myfield', isset($databaseData['myfield']) ? $databaseData['myfield'] : '');

$databaseData will contain the data from the database which you have loaded in the controller and passed to view

$data['databaseData']= $this->my_model->find($parameters);

$this->load->view('myformview',$data);


回答2:

You try this code

set_value('<field_name>', @$field_from_db);

@ is used to skipps errors if field is not set