如何在编辑页面中回显数据库中的下拉值

时间:2016-10-26 07:20:58

标签: php codeigniter

我想在编辑模式下回显下拉列表的数据库值。但在编辑页面中,它不显示以前选择的下拉值。现在我想将数据库值回显到下拉列表。我该怎么做。我的视图控制器和模型如下。请任何人都可以帮助我。

查看

<!--//Address--> 
<tr><td><?php echo '<label for="heading" class="col-sm-0 control-label">Address </label>';?></td>
    <td class="col-lg-10">
<?php
$data = array(
          'name'        => 'address',
          'value'          => $post->address,
          'class'       => 'form-control',
          'style'       => 'width:140%',
          'placeholder' => 'Address of Your Site'
        );
echo form_input($data);
echo '<br>';
?>
        </td>
</tr>       
<!--//Address-->

    <?php foreach ($single as $post):?>
        $attributes = 'class = "form-control" id = "client" style="width:140%; height:35px;"';
        $buildings = array('-SELECT-'=>'-SELECT-', 'House'=>'House', 'Building Complex'=>'Building Complex', 'Commercial Building'=>'Commercial Building', 'Hotel'=>'Hotel','Hospital'=>'Hospital' );
        echo form_dropdown('building', $buildings, $post->building,set_value('building'), $attributes);
    ?>

控制器

function edit_single(){
$id = $this->uri->segment(3);
//load project details according to relevent id
$data['single'] = $this->project_registration_details->show_pro_id($id); 
$this->load->view('client/project_registration_edit_view',$data);
}

模型

function show_pro_id($id){
$this->db->select('*');
$this->db->from('project_registration');
$this->db->join('client', 'client.client_id = project_registration.reg_client_id');
$this->db->where('project_registration.id',$id);
$query = $this->db->get();
$result = $query->result();
return $result;
}

1 个答案:

答案 0 :(得分:0)

使用

set_value('building',$post->building)

form_dropdown函数的所选部分中。

尝试..

echo form_dropdown('building', $buildings,set_value('building',$post->building), $attributes);

更新了答案

<?php foreach($single as $post) { ?>
    <select name="building" class = "form-control" id = "client" style="width:140%; height:35px;">
        <?php 
        $buildings = array('-SELECT-'=>'-SELECT-', 'House'=>'House', 'Building Complex'=>'Building Complex', 'Commercial Building'=>'Commercial Building', 'Hotel'=>'Hotel','Hospital'=>'Hospital' );
        foreach($buildings as $key => $building) { ?>
             <option value="<?php echo $key; ?>" <?php echo set_select('building', $post->building); ?>><?php echo $building; ?></option>
        <?php } ?>
    </select>
<?php } ?>