Question

I have a database table called input_types with a various input types:

[id]  [input_type_Name]
 1    text
 2    radio
 3    checkbox
 4    select
 ...  ...

I want to retrieve the names from the table and put them in an array so that I can then use form_dropdown to show them to the user in a dropdown.

The problem with how I'm doing it now is that I create optiongroups.

How I do it now:

Model

function get_inputTypes() {
    $this->db->select('input_type_name');
    $query = $this->db->get('input_types');

    if($query->num_rows() > 0) {
        return $query->result_array();
    }

    else {
        return false;
    }
}

Controller

$results = $this->survey_model->get_inputTypes();
$data['inputTypes'] = $results;

View

<label for="inputType">Input type:</label>
<?php echo form_dropdown('inputType', $inputTypes); ?>

This however doesn't create the desired effect. My dropdown gets populated, but because I have a multidimensional array the dropdown has optgroups.

I just want to have my selected data in a simple array. Why is this so freaking hard in CodeIgniter and php in general.

C# is much easier :/

Solution

The solution is very simple, use a foreach to loop through the multidimensional array in the model:

foreach($query->result() as $input_type) {
    $data[] = $input_type->input_type_name;
}

return $data;
Was it helpful?

Solution 2

The solution is very simple, use a foreach to loop through the multidimensional array in the model:

foreach($query->result() as $input_type) {
    $data[] = $input_type->input_type_name;
}

return $data;

OTHER TIPS

What do you mean by "This however doesn't create the desired effect" ?

Make sure you pass the $data to your view:

$this->load->view('viewname', $data);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top