我有一个从数据库中检索到的扇区列表,我试图根据用户仅填充选定的扇区。有 7 个扇区(如下所示)。
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>
控制器:
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;
现在,当用户去编辑他们的个人资料时,我正在尝试预先填充用户之前选择的复选框。
我对如何在创建帐户时重新填充这些部门感到困惑。我尝试了以下方法。
控制器:
$data['assigned_sectors'] = $this->info_model->getAssignedSectors();
foreach($data['assigned_sectors'] as $sector)
{
$id_str = array(
'ID' => $sector->sector_ID
);
}
print_r($id_str);
以下是 '$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 ) )
我尝试将两者都添加到 foreach 复选框 html 但没有运气
set_checkbox('sector[]', $data['assigned_sectors']->sector_ID) //not working
set_checkbox('sector[]', $id_str->ID) //not working
我可能忽略了一些简单的事情,但任何帮助将不胜感激。谢谢!
更新:
$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>";
如果从 7 个机构部门中选择了 2 个机构部门,则列表重复两次,3 个结果相同,依此类推