Code Igniter - form_dropdown selecting correct value from the database

前端 未结 5 1344
傲寒
傲寒 2021-01-05 18:52

Im having a few problems with the form_dropdown function in CodeIgniter .... My application is in 2 parts, a user goes in, enters a form and submits it .... once its submitt

相关标签:
5条回答
  • 2021-01-05 19:43

    The form_dropdown function has a third parameter for the selected option. Use it like this:

    <?php echo form_dropdown('piece_type', 
            array(
                'type1' => 'Firts type',
                'type2' => 'Second option'
                $selected_value, 
                'id = "piece_type"') ?>
    
    0 讨论(0)
  • 2021-01-05 19:43

    For update case, you have pass corresponding value to view, if passing variable is like $ind_post(from controller ) then write this code like:

    <?php echo form_dropdown('salaries', $salaries, $ind_post->salaries,'');  ?>
    
    0 讨论(0)
  • 2021-01-05 19:46

    one nasty solution to select the <option> element of <select> generated by form_dropdown() function of the form_helper is using the post input sended. I made this because any solutions I found doesn't display the value that the user select in the form neither set_selected nor set_vaule. Well, in my controller I have:

    $countries = $this->country_model->get_dropdown_array(); // The array have something like $countries[COUNTRY_ID] = COUNTRY_NAME
    $data['countries']=$countries;
    

    In my view:

    $selected_country = $this->input->post('country');
    echo form_dropdown('country',$countries,$selected_country);
    

    And works fine !!! :)

    0 讨论(0)
  • 2021-01-05 19:47

    I had the same problem but i have overcome on this problem using code igniter syntex. Here is the solution. Fisrt step Before the loop initialize two arrays

    $options = array();
    $select = array();
    

    Then in the loop write this instruction

    foreach($result->result_array() as $row)
    {
        /////////Your Condition ////////////
        if($row['id'] == $myarray['mycolumn'])
        {            
            $options [$row['id']] = $row['salaryrange'];
            $select = $row['id'] ; 
        }else{
            $options [$row['id']] = $row['salaryrange'];
        }
    }
    

    Now

    echo form_dropdown('dropdown_name' , $options , $select);
    

    It is working ok

    0 讨论(0)
  • 2021-01-05 19:50

    According to Codeigniter documentation

    The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. You can also pass an array of multiple items through the third parameter, and CodeIgniter will create a multiple select for you.

    Your admin controller should have something like

    $data['selected'] = $this->salary_expectation->get_salary_selected();
    

    According to this, the admin view should be like this

    <?php echo form_dropdown('salaries', $salaries, $selected_value);  ?>
    
    0 讨论(0)
提交回复
热议问题