I have this code inside my view.
$country = array(
'id' => 'country',
'name' => 'country',
'value' => get_user_info('country'),
'size' => 30
);
<tr>
<td><?php echo form_label('Country', $country['id']); ?></td>
<td><?php echo form_input($country); ?></td>
<td style="color: red;"><?php echo form_error($country['name']); ?><?php echo isset($errors[$country['name']])?$errors[$country['name']]:''; ?></td>
</tr>
The get_user_info() is a function defined in my form_helper like this:
Form_Helper.php
if(! function_exists('get_user_info')){
function get_user_info($field)
{
$ci = & get_instance();
$ci->load->model('users');
return $ci->users->get_user_profile($field);
}
}
As you can see , inside this function I access the database through the users Model.
User_Model
function get_user_profile($field)
{
$user_id = $this->session->userdata('user_id');
$this->db->select($field);
$this->db->where('user_id',$user_id);
$query = $this->db->get($this->profile_table_name);
if($query->num_rows()==1)return $query->row();
return NULL;
}
The idea is to auto fill the Country field of the form while the page loads.
But in the view I am getting this error
A PHP Error was encountered
Severity: Warning
Message: htmlspecialchars() expects parameter 1 to be string, object given
Filename: helpers/form_helper.php
Line Number: 646
What can be the problem ?
Can someone knows what is happening ? Or has someone did a such a thing in the past ?
Is it the right way to access the Model within a helper function ?
Thanks
EDIT
To do better and faster I simply call the model from my controller and then pass the different values to the view.
Controller
$d = $this->users->get_user_profile('country, telephone, city, street, address, town');
$d2 = Array(
'telephone' => $d->telephone,
'country' => $d->country,
'city' => $d->city,
'street' => $d->street,
'address' =>$d->address,
'town' => $d->town);
$this->template->write_view('contentw','dashboard/profile', $d2);
$this->template->render();
So I delete the function I added to my Helper file.
This method is working for me.
Thank you all for your answers
Generally helpers are used as Global functions to do some simple work. Most people would say it is wrong to invoke a Model in the middle of a helper. Helpers should be used like the PHP function
explode; it has a single task, receives input, and provides output based on the input very mechanically.It might be better instead for the controller to access the model and get that value, then pass it into the view and use it directly
As for the error:
You are probably getting that error because you are returning
$query->row()instead of an actual value in that field.$query->row()is most likely an object and$query->row()->countryis the actual value