【问题标题】:Get selected values from combo box PHP Codeigniter从组合框 PHP Codeigniter 中获取选定的值
【发布时间】:2023-03-02 21:13:01
【问题描述】:

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

在我的数据库中,我的表“location_category”包含字段:id_category (primarykey)、name_category、icon_category

还有带有字段的表“位置”:id_location(主键)、name_location、id_category(表 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>

这是 location_edit.php 的控制器

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);
	}

【问题讨论】:

  • 请按照您的期望明确您的问题。其实我没看懂你的问题。您想在编辑时选择下拉值吗??
  • Helo @KaziNayem 是的,我在编辑时使用下拉选择。但是当我编辑数据时,下拉值应该返回到添加数据时显示的默认值。
  • 你能解释一下什么是数据,id_data是你的get_where吗?您在上面提到您的表是“位置”和“位置类别”,但数据和 id_data 是什么意思
  • @KaziNayem 抱歉打错了。我的意思是数据是位置表,id_data 是 id_location。所以有 2 个表,location 和 location_category。 :)
  • 尝试结构化编码。使用模型进行查询和控制器获取模型数据。

标签: php html mysql codeigniter combobox


【解决方案1】:

如果您的隐藏值完美运行,那么我认为下面的代码会对您有所帮助。

<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>

【讨论】:

  • 这是我的荣幸,亲爱的。继续。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 2011-10-17
相关资源
最近更新 更多