这始终从我的数据库中选择最后一条记录。
这是我的选择标记:
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){
$selected= '';
if($t_proyek_kd_proyek == $value->nm_proyek){
$selected = 'selected';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" echo ' selected="selected"'; >
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
这是我的控制者:
$row = $this->T_cost_model->get_by_id($id);
if ($row) {
$data = array(
'button' => 'Update',
'action' => site_url('konsultan/cost/update_action'),
'kd_cost' => set_value('kd_cost', $row->kd_cost),
'investasi_awal' =>rupiah(set_value ('investasi_awal', $row->investasi_awal)),
'b_operasional' => rupiah(set_value('b_operasional', $row->b_operasional)),
'g_cost' => rupiah(set_value('g_cost', $row->g_cost)),
'disbenfit' => rupiah(set_value('disbenfit', $row->disbenfit)),
'pwc' => set_value('pwc', $row->pwc),
't_proyek_kd_proyek' => set_value('t_proyek_kd_proyek', $row->t_proyek_kd_proyek),
);
$this->load->view('konsultan/t_cost_form', $data);
答案 0 :(得分:0)
像这样更改您的 <select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
foreach($t_cost->result() as $value){
$selected= '';
if($t_proyek_kd_proyek == $value->nm_proyek){
$selected = ' selected="selected" ';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" <?= $selected?> >
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
html代码值
{{1}}希望它会有所帮助
答案 1 :(得分:0)
在您的HTML中,您的上一个option
代码已明确设置selected="selected"
:
option value="<?php echo $value->kd_proyek; ?>" <?php echo 'selected="selected"'; ?>
将条件包裹在条件中并且循环中的选定选项应该有效。
option value="<?php echo $value->kd_proyek; ?>" <?php if ($selected) echo 'selected="selected"'; ?>