Code Igniter - form_dropdown select the correct value from the database

I have several problems with the form_dropdown function in CodeIgniter .... My application consists of 2 parts, the user logs in, logs into the form and submits it .... after its submitted admin can login and edit what people form , and then save it to the database.

So, to display the drop-down list in the original form, im uses the following (all parameters in the drop-down list come from the database)

Model:

    function get_salaries_dropdown()
{
    $this->db->from($this->table_name);
    $this->db->order_by('id');
    $result = $this->db->get();
    $return = array();
    if($result->num_rows() > 0){
            $return[''] = 'please select';
        foreach($result->result_array() as $row){
            $return[$row['id']] = $row['salaryrange'];
        }
    }
    return $return;
}

Then in the controller:

$data['salaries'] = $this->salary_expectation->get_salaries_dropdown();

Then, finally, the view:

<?php echo form_dropdown('salaries', $salaries, set_value('salaries', $salaries));  ?>

This bit works great when displaying a drop-down list populated with values ​​for user selection.

So, when the user selects a value, he removes save, it is saved in the database.

, , im, , , , , ?

,

+5
5

Codeigniter

, , , . , CodeIgniter - .

-

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

, :

<?php echo form_dropdown('salaries', $salaries, $selected_value);  ?>
+3

<option> <select>, form_dropdown() form_helper, . , , , set_selected, set_vaule. , :

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

:

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

!!!:)

+1

form_dropdown . :

<?php echo form_dropdown('piece_type', 
        array(
            'type1' => 'Firts type',
            'type2' => 'Second option'
            $selected_value, 
            'id = "piece_type"') ?>
0

I had the same problem, but I overcame this problem using code igniter syntax. Here is the solution. Fisrt Step Initialize Two Arrays Before a Loop

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

Then write this instruction in a loop

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);

He is working fine

0
source

For the update case, you have the appropriate value for viewing, if the transfer variable is similar to $ ind_post (from the controller), then write this code as follows:

<?php echo form_dropdown('salaries', $salaries, $ind_post->salaries,'');  ?>
0
source

All Articles