I am new to codeigniter and trying to make a dropdown using form helper. 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...).
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 (i.e. 0, 1, 2) based on which selection is made, instead of equal to the actual 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;
}