我正在使用Codeigniter编写一个小应用程序,到目前为止遇到了一些问题!我有一个从db表填充的下拉列表。下拉列表已填充并正确显示,但是当我尝试将其内容(值)保存到新的db表时,它将保存下拉列表中的位置,而不是显示的值。
视图中的部分表单:
<?php echo form_open('manage/create_sold_vehicle_record'); ?>
Registration Number
<br>
<?php echo form_dropdown('reg_no', $vehicle_list, 'large'); ?>
控制器的相关部分:
function sell_vehicle()
{
$this->load->helper('form');
$this->load->helper('url');
$id = $this->uri->segment(3);
$this->load->model('MVehicle','',TRUE);
$this->load->model('manage_model','',TRUE);
$vehicles_qry = $this->MVehicle->listSaleVehicles();
$vehicle_list = array();
foreach ($vehicles_qry->result() as $vehicle)
{
$vehicle_list[] = $vehicle->reg_no;
}
$data['vehicle_list'] = $vehicle_list;
$data['main_content'] = 'sell_stock_vehicle';
$this->load->view('includes/template', $data);
}
这是模特:
function create_sold_vehicle_record()
{
$new_vehicle_insert_data = array(
'reg_no' => $this->input->post('reg_no'), <-- This is saved to db, but is the position in dropdown list, not the value shown.
'sold_by' => $this->input->post('staff'),
'bought_by' => $this->input->post('customer'),
'sale_price' => $this->input->post('sale_price'),
'comm_rate' => $this->input->post('comm_rate'),
'total_comm' => $this->input->post('total_comm'),
'sale_date' => $this->input->post('date')
);
$insert = $this->db->insert('sold_vehicles', $new_vehicle_insert_data);
return $insert;
}
所以,而不是例如保存&#39; OU03XHC&#39;作为db中的reg_no值,它正在保存&#39; 1&#39; (或者值在下拉列表中的位置)。
答案 0 :(得分:0)
$vehicle_list[]
需要是一个关联数组
$vehicle_list = array();
foreach ($vehicles_qry->result() as $vehicle)
{
$vehicle_list[$vehicle->reg_no] = $vehicle->reg_no;
}
现在,form_dropdown()还会在下拉列表中生成选项值(0,1等除外)
答案 1 :(得分:0)
替换
$vehicle_list = array();
foreach ($vehicles_qry->result() as $vehicle)
{
$vehicle_list[] = $vehicle->reg_no;
}
TO
$vehicle_list = array();
foreach ($vehicles_qry->result() as $vehicle)
{
$vehicle_list[$vehicle->reg_value] = $vehicle->reg_no;
}
“$ vehicle-&gt; reg_value”veriable是您要存储在数据库中的值