简体   繁体   English

使用form_helper输入的Codeigniter下拉表单是索引/键,而不是所选值

[英]Codeigniter dropdown form with form_helper input is index/key instead of the value selected

I am new to codeigniter and trying to make a dropdown using form helper. 我是Codeigniter的新手,并尝试使用表单助手进行下拉。 I have made regular input forms but now I would like to retrieve options from my database to populate my dropdown. 我已经制作了常规的输入表单,但是现在我想从数据库中检索选项以填充下拉列表。 It seemed like I had most of this working(If I make the input a regular text field I can make changes), but when I try to run my update function I get a database error because for some reason I'm getting the index/key of the selection rather than the actual value that I am trying to use to update the database(I'm also trying to use jquery to dynamically affect another dropdown but am not quite there yet...). 似乎我已经完成了大部分工作(如果我将输入设置为常规文本字段,则可以进行更改),但是当我尝试运行更新功能时,却收到了数据库错误,因为出于某种原因,我得到了索引/选择的关键,而不是我试图用来更新数据库的实际值(我也试图使用jquery动态影响另一个下拉列表,但还不完全到此……)。

My part of my form that is related to this question: 我表格中与该问题有关的部分:

echo form_label('Stage');
echo form_dropdown('stage_name', $stage_options, $stage[0]->stage_name, 
'id=
stagesDrp"');

My model methods that are related: 我相关的模型方法:

function get_stage_options($id){
  $this->db->select('stages.stage_name')->from('stages')-     
>join('prices', 'prices.id=stages.price_id')- 
>join('events','events.price_id=prices.id')->where('events.id', $id);
  $stage_options=$this->db->get();
  if($stage_options->num_rows()>0)
  {
    foreach($level_options->result() as $row)
    {
      $options[]=$row->stage_name;
    }
    return $options;
  }

 function update_stage($id){
  $stage=array(
    'stage_name'=>$this->input->post('stage_name')
  );

  $this->db->where('events.id', $id);
  $this->db->update('stages', $stage_name);

}

When I try to submit my update form, I get a database error because it's trying to set stage_name = an index value (ie 0, 1, 2) based on which selection is made, instead of equal to the actual stage_name. 当我尝试提交更新表单时,出现数据库错误,因为它试图根据做出选择的方式设置stage_name =一个索引值(即0、1、2),而不是等于实际的stage_name。

Maybe I am missing a simple step? 也许我错过了一个简单的步骤? I really appreciate the help! 我非常感谢您的帮助!

Set the name as index to the array. 将名称设置为数组的索引。 Try with - 尝试-

foreach($level_options->result() as $row)
{
  $options[$row->stage_name] = $row->stage_name;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM