I have a list of sectors that I have retrieved from a database and I am trying to populate only the selected sectors depending on the user. There are 7 sectors (shown below).
HTML:
<p class="sector"><span>North East</span><input type="checkbox" class="sector-list" name="sector[]" value="1" /></p>
<p class="sector"><span>City of Sunderland</span><input type="checkbox" class="sector-list" name="sector[]" value="2" /></p>
<p class="sector"><span>Sunderland East</span><input type="checkbox" class="sector-list" name="sector[]" value="3" /></p>
<p class="sector"><span>Sunderland North</span><input type="checkbox" class="sector-list" name="sector[]" value="4" /></p>
<p class="sector"><span>Sunderland West</span><input type="checkbox" class="sector-list" name="sector[]" value="5" /></p>
<p class="sector"><span>Coalfield</span><input type="checkbox" class="sector-list" name="sector[]" value="6" /></p>
<p class="sector"><span>Washington</span><input type="checkbox" class="sector-list" name="sector[]" value="7" /></p>
CONTROLLER:
foreach($data['all_sectors'] as $sectors):
echo"<p class='service'><span>$sectors->sector</span><input type='checkbox' class='sector-list' name='sector[]' value='$sectors->ID' /></p>";
endforeach;
Now when the user goes to edit their profile I am trying to pre populate the checkboxes that the user has selected previously.
I am confused as to how I can repopulate the sectors when they created an account. I have tried the following.
CONTROLLER:
$data['assigned_sectors'] = $this->info_model->getAssignedSectors();
foreach($data['assigned_sectors'] as $sector)
{
$id_str = array(
'ID' => $sector->sector_ID
);
}
print_r($id_str);
Below is the result for '$data['assigned_sectors']'
Array ( [0] => stdClass Object ( [agency_ID] => 2 [sector_ID] => 3 [ID] => 3 [sector] => Sunderland East ) [1] => stdClass Object ( [agency_ID] => 2 [sector_ID] => 4 [ID] => 4 [sector] => Sunderland North ) )
I have tried adding both to the foreach checkbox html but no luck
set_checkbox('sector[]', $data['assigned_sectors']->sector_ID) //not working
set_checkbox('sector[]', $id_str->ID) //not working
I am probably overlooking something simple but any help would be greatly appreciated. Thanks!
UPDATE:
$this->load->model('admin/info_model');
$data['agency_sectors'] = $this->info_model->getAgencySectors($agency);
$this->load->model('admin/agencies_model');
$data['all_sectors'] = $this->agencies_model->getAgencySectors($agency);
echo"<p id='sectors'><label for='sectors'>Sector</label></p>";
echo"<div id='sector_list' style='float: left;width: 70%;'>";
foreach($data['agency_sectors'] as $sector):
foreach($data['all_sectors'] as $sectors):
$selected_text = ($sector->sector_ID === $sectors->ID)
? " checked='checked'"
: '' ;
echo"<p class='service'><span>$sectors->sector</span><input type='checkbox' class='sector-list' name='sector[]' value='$sectors->ID' " . $selected_text . "/></p>";
endforeach;
endforeach;
echo"</div>";
If there are 2 agency sectors selected out of the 7 it duplicates the list twice, same for 3 results and so forth