从存储的记录中填充下拉列表

时间:2015-12-27 01:51:31

标签: php codeigniter drop-down-menu

我有来自硬编码数组的数据,该数据进入下拉列表,然后添加到SQL数据库中。我正在尝试编写一个更新记录的函数。在我的更新记录页面上,它将数据填充到表单字段中。但是,我无法弄清楚如何使下拉列表填充该特定记录的存储内容。我正在使用CodeIgniter框架。有问题的下拉列表是列车方向字段。

模型

public function edit_recrew()
{
    $this->db->where('train_id', $this->uri->segment(3));
    $query = $this->db->get('trains_road');
    return $query->result();
}

public function update_recrew($data)
{
    $this->db->where('train_id', $data['train_id']);
    $this->db->update('trains_road', $data);
}

查看

<?=
    form_open('atis/update_recrew');
    foreach ($record as $value) {
?>
        <input type="hidden" name="train_id" title="train_id" value="<?php echo $value->train_id; ?>">
        <tr>
            <td>Train Direction:</td>
            <?php
                $train_direction = array('.'$value->train_direction'.' => '.'$value->train_direction'.',
                    'North' => 'North',
                    'South' => 'South',
                    'East' => 'East',
                    'West' => 'West'
                );
            ?>
            <td><?= form_dropdown('txttrain_direction', $train_direction); ?></td>

控制器

public function update_recrew()
{
    $data = array(
        'train_id' => $this->input->post('train_id'),
        'train_no' => $this->input->post('train_no'),
        'train_location' => $this->input->post('train_location'),
        'train_symbol' => $this->input->post('train_symbol'),
        'train_direction' => $this->input->post('train_direction'),
        'train_hpt' => $this->input->post('train_hpt'),
        'train_crew' => $this->input->post('train_crew'),
        'train_status' => $this->input->post('train_status')
    );
    $this->load->model('atisroad_model');
    $this->atisroad_model->update_recrew($data);
    if ($this->db->affected_rows() > 0)
    {
        redirect(base_url() . 'atis/recrews');
    }
    else
    {
        echo 'failure';
    }

1 个答案:

答案 0 :(得分:1)

当您调用form_dropdown()函数时,可以将当前值作为第3个参数传递。

$options = array(
    'small'  => 'Small Shirt',
    'med'    => 'Medium Shirt',
    'large'   => 'Large Shirt',
    'xlarge' => 'Extra Large Shirt'
);

echo form_dropdown('shirts', $options, 'large');

当下拉列表中的任何一个选项匹配时,将选择第3个参数。

See the docs for the CodeIgniter form helper