我的数据库中有这两个表:
tbl_province
id | id_prov | prov_name
和
tbl_district
id | id_dist | id_prov | dist_name
和
tbl_myhome
id | id_prov | id_dist | name | address
我有这个控制器用于编辑&更新:
public function edit()
{
$this->load->model('m_login');
$level = $this->session->userdata('level');
$data['menu'] = $this->m_login->get_menu_for_level($level);
$data['title'] = 'District Data';
$data['content'] = 'district/v_edit_dist';
$id = $this->uri->segment(3);
$this->db->where('id_dist',$id);
$q = $this->db->get('area_db');
if($q->num_rows()>0){
foreach($q->result() as $row){
$data['id'] = $row->id;
$data['id_dist'] = $id_dist;
$data['dist_name'] = $dist_name;
}
} else {
$data['id'] = '';
$data['id_prov'] ='';
$data['id_dist'] ='';
$data['dist_name'] ='';
}
$this->load->view('v_home',$data);
}
这是我在Dropdown上的v_edit_dist来源:
<?php
$q = $this->db->get('tbl_myhome');
foreach($q->result() as $row){
$distname = $row->dist_name;
?>
<select name="id_dist" id="id_dist" class="form-control">
<option selected="<?php echo $id_dist;?>"><?php echo $dist_name;?></option>
</select>
当我尝试按行编辑我的数据时,下拉菜单中的所有数据都显示出来,所有的地区名称都显示出来,并且表单中的地区名称错误,它显示的是第一个地区ID,但是实际上在数据库中数据是第五个区域id。
我只是混淆了如何在下拉列表中将区域名称与数据库中的区域名称进行匹配,并且并非所有区域名称都显示并弄乱了CSS。请帮帮......我很绝望。
谢谢。
答案 0 :(得分:0)
<select name="id_dist" id="id_dist" class="form-control">
<option value="<?php echo $id_dist;?>"><?php echo $dist_name;?></option>
</select>
答案 1 :(得分:0)
///// In controller file
public function edit()
{
$this->load->model('m_login');
$level = $this->session->userdata('level');
$data['menu'] = $this->m_login->get_menu_for_level($level);
$data['title'] = 'District Data';
$data['content'] = 'district/v_edit_dist';
$id = $this->uri->segment(3);
$q = $this->db->get('tbl_district');
$district = array();
if($q->num_rows()>0){
foreach($q->result() as $row){
$district[]= $row;
}
}
$data['id_dist'] = $id;
$data['district']= $district;
$this->load->view('v_home',$data);
}
// in view file
<select name="id_dist" id="id_dist" class="form-control">
<?php
if(count($district)){
foreach($district as $item){
?>
<option value="<?php echo $item->id_dist; ?>" <?php if($item->id_dist==$id_dist) echo 'selected';?>><?php echo $item->dist_name;?></option>
<?php
}
}
?>
</select>
答案 2 :(得分:0)
在您的视图页面上使用此代码
<select name="id_dist" id="id_dist" class="form-control">
<?php
$q = $this->db->get('tbl_myhome');
$result = $q->result();
foreach($result as $row){
?>
<option value="<?php echo $row->id_dist;?>" <?php if($row->id_dist==$id_dist){ echo 'selected';} ?>><?php echo $row->dist_name;?></option>
<?php } ?>
</select>