[英]Populate a drop-down list from a stored record
我有來自硬編碼數組的數據,該數組進入下拉列表,然后添加到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';
}
您可以在調用form_dropdown()
函數時將當前值作為第三個參數傳遞。
$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt'
);
echo form_dropdown('shirts', $options, 'large');
呈現下拉菜單時,將選擇與第三個參數匹配的任何選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.