I’m working with a database table named user_preferences
that stores information about user property searches. The table has columns like user_id
, budget_min
, budget_max
, and home_type
. The home_type
column contains comma-separated values like “Apartment,Condo,Townhouse”.
Here’s my model function that retrieves the data:
public function getUserPreferences($userId)
{
$result = $this->db->get_where('user_preferences', array('user_id' => $userId));
return $result->row();
}
In my controller, I’m calling this function:
$data['userInfo'] = $this->preference_model->getUserPreferences($userId);
$this->layout->view('admin/users/edit_preferences', $data);
I want to split the comma-separated home_type
values in the controller so I can pre-select the appropriate checkboxes in my view. My view has checkboxes like this:
<label>
<input type="checkbox" value="apartment" name="hometype[]">
Apartment
</label>
<label>
<input type="checkbox" value="condo" name="hometype[]">
Condo
</label>
<label>
<input type="checkbox" value="townhouse" name="hometype[]">
Townhouse
</label>
How can I explode the comma-separated values and pass them to my view so the checkboxes get pre-selected based on the database values?