0

我有一个从数据库中检索到的扇区列表,我试图根据用户仅填充选定的扇区。有 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 个结果相同,依此类推

4

2 回答 2

3

name属性应与第一个参数匹配set_checkbox

改变:

set_checkbox('sector', '1') 

至:

set_checkbox('sector[]', '1')

******更新****

我之前的回答是错误的。您不能使用set_checkbox数据库中的值设置复选框选中状态。请参阅@Raidenace 的回答

您必须通过检查循环中的值是否相同来设置它:

而不是这段代码:

<p class='sector'><span>$sectors->sector</span>
  <input type='checkbox' class='sector-list' name='sector[]' 
    value='1' " 
      . set_checkbox('sector', $data['assigned_sectors']->sector_ID) . " />
</p>

试试这个代码:

$selected_text = ($data['assigned_sectors']->sector_ID === $sectors->ID) 
                   ? " checked='checked'" 
                     : '' ;
<p class='sector'><span>$sectors->sector</span>
  <input type='checkbox' class='sector-list' name='sector[]' 
    value='$sectors->ID' " . $selected_text . " />
</p>
于 2013-05-07T16:18:03.233 回答
3

好的,这似乎不直观,但是 set_checkbox() 函数“允许您通过传递的 POST 数组中的值设置复选框的选定值。因此,这意味着将复选框恢复为用户在表单提交,在表单得到验证的用例中,由于验证错误,用户返回到相同的表单。我认为这不是你想要完成的事情。

我必须从他们的功能定义中弄清楚

/**
 * Set Checkbox
 *
 * Let's you set the selected value of a checkbox via the value in the POST array.
 * If Form Validation is active it retrieves the info from the validation class
 *
 * @access  public
 * @param   string
 * @param   string
 * @param   bool
 * @return  string
 */
if ( ! function_exists('set_checkbox'))
{
    function set_checkbox($field = '', $value = '', $default = FALSE)
    {
        $OBJ =& _get_validation_object();

        if ($OBJ === FALSE)
        {
            if ( ! isset($_POST[$field]))
            {
                if (count($_POST) === 0 AND $default == TRUE)
                {
                    return ' checked="checked"';
                }
                return '';
            }

            $field = $_POST[$field];

            if (is_array($field))
            {
                if ( ! in_array($value, $field))
                {
                    return '';
                }
            }
            else
            {
                if (($field == '' OR $value == '') OR ($field != $value))
                {
                    return '';
                }
            }

            return ' checked="checked"';
        }

        return $OBJ->set_checkbox($field, $value, $default);
    }
}
于 2013-05-07T16:18:38.350 回答