从组合框PHP Codeigniter中获取选定的值

时间:2017-04-23 13:07:57

标签: php html mysql codeigniter combobox

您好我正在使用Codeigniter框架创建 Web GIS 。 我在更新位置数据时遇到了问题。

在我的数据库中,我有表" location_category" with field:id_category(primarykey),name_category,icon_category

还有桌子"位置" with field:id_location(primarykey),name_location,id_category(foreignkey to table location_category),lat,long。

所以,问题是我更新了它的类别组合框中的数据位置,它没有指向选定的位置类别。

这是location_edit.php

查看



<div class="form-group">
                  <label>Edit Location</label>
                  <br/>
                   <input name="id_location" type="hidden" value="<?=$d['id_location'] ?>">
                  <select name="location_category" class="form-control select2" style="width: 40%;">
                   <-- The problem should be here -->
                    <?php foreach ($dj->result() as $r) {
                      echo "<option value='$d->id_category'>$r->nama_category</option>";
                    } ?>
                  </select>
                </div>
                <div class="form-group">
                  <label>Name</label>
                  <input name="name_location" type="text" id="name_location" class="form-control" style="width:40%;" value="<?=$d['name_location'] ?>" required="">
                </div>
<div class="form-group">
                  <label>Latitude</label>
                  <input name="latitude" type="text" id="latitude" class="form-control" style="width:40%;" value="<?=$d['latitude'] ?>" required="">
                </div>
                 <div class="form-group">
                  <label>Longitude</label>
                  <input name="longitude" type="text" id="longitude" class="form-control" style="width:40%;" value="<?=$d['longitude'] ?>" required="">
                </div>
&#13;
&#13;
&#13;

这是location_edit.php的控制器

&#13;
&#13;
public function Location_edit($id)
	{
		$data['c'] = $this->db->get('location_category');
		$data['d'] = $this->db->get_where('location',array('id_location'=>$id))->row_array();
		$data['dj'] = $this->db->query("SELECT l.id_location, l.id_category, k.name_category FROM location as l, category as c WHERE l.id_category=c.id_category");
		$this->template->load('back-end/_template','back-end/_location_edit',$data);
	}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

如果您的隐藏价值完美无缺,那么我认为下面的代码会让您惊讶。

<select name="location_category" class="form-control select2" style="width: 40%;">
   <option value="">Select Something</option>
   <?php foreach($dj->result() as $r): ?>
   <option value="<?php echo $r->id_category; ?>" <?php if($r->id_category==$d['id_location']) { ?> selected="selected" <?php } ?>><?php echo $r->nama_category; ?></option>
   <?php endforeach; ?> 
</select>