我是 codeigniter 的新手,我制作了一个 Web 应用程序,其中包含三个下拉列表。我的问题是,当我现在保存到数据库时,保存的是 id 而不是下拉列表中的值。这是我的下拉菜单模型代码:
function get_store()
{
$this->db->select('ID');
$this->db->select('StoreName');
$this->db->from('store');
$query = $this->db->get();
$result = $query->result();
// array to store id and storename
$id = array('-SELECT-');
$storename = array('-SELECT-');
for($i = 0;$i < count($result);$i++)
{
array_push($id,$result[$i]->ID);
array_push($storename,$result[$i]->StoreName);
}
return $store_result = array_combine($id,$storename);
}
function get_supplier()
{
$this->db->select('ID');
$this->db->select('SupplierName');
$this->db->from('supplier');
$query = $this->db->get();
$result = $query->result();
// array to store id and storename
$id = array('-SELECT-');
$suppliername = array('-SELECT-');
for($i = 0;$i < count($result);$i++)
{
array_push($id,$result[$i]->ID);
array_push($suppliername,$result[$i]->SupplierName);
}
return $supplier_result = array_combine($id,$suppliername);
}
function get_operation()
{
$this->db->select('ID');
$this->db->select('OperationName');
$this->db->from('operation');
$query = $this->db->get();
$result = $query->result();
// array to store id and storename
$id = array('-SELECT-');
$operationname = array('-SELECT-');
for($i = 0;$i < count($result);$i++)
{
array_push($id,$result[$i]->ID);
array_push($operationname,$result[$i]->OperationName);
}
return $operation_result = array_combine($id,$operationname);
}
这是我在 Controller 中使用的代码:
$data['store'] = $this->wip_model->get_store();
$data['suppliersname'] = $this->wip_model->get_supplier();
$data['operation'] = $this->wip_model->get_operation();
这就是我认为的使用方式:
<?php $attributes = 'class="form-control" id="store"';
echo form_dropdown('store',$store,set_value('store',$store),$attributes);?>
请帮助我,因为我被困住了......
这是您需要的吗?
<div class="form-group">
<label>Store Name</label>
<?php $attributes = 'class="form-control" id="store"';
echo form_dropdown('store',$store,set_value('store',$store),$attributes);?>
<span class="text-danger"><?php echo form_error('store');?></span>
</div>
最佳答案
看这个
<select name="agama" id="agama">
<option value="1">store 1</option>
<option value="2">store 2</option>
</select>
在此代码中,当我们将值发布到 Controller 时,我们得到值 1 或 2。 你的问题也有同样的情况。你可以通过两种方式做到这一点。 其一是。当你获得 id 后,你可以编写一个模型查询来查找该 id 的名称。 否则,您可以将选项值设置为下拉列表中的值
关于php - 使用 codeigniter 传递下拉列表的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35357749/