Welcome Guest, Not a member yet? Register   Sign In
Fill dropdown with an option array
#1

[eluser]Tenter[/eluser]
Hello,

I have a problem with my code.. i cant figure out how to get it work.. The problem is that i have this array wich i get from my model:

Code:
public function get_all_dansetyper()
    {
        $sql = "SELECT
                        id,
                        danseart
                    FROM
                        dansearter
                    ORDER BY
                        id";
      
            $result = $this->db->query($sql);
            return $result->result();
    }

Afterwards i use this get funktion in my controller:

Code:
public function index()
{
  $data['dansetyper'] = $this->danse_model->get_all_dansetyper();
  $this->load->view('snippets/header');
  $this->load->view('tilmaelding', $data);
  $this->load->view('snippets/footer');
}

And this variable "dansetyper" ill send to my view file with the array in, this array is an object and i cant just put it into my:
Code:
<php
  $options = "";
  $options .= "array(";
  foreach($dansetyper as $dansetype):
        $options .= "'" . $dansetype->id . "' => '" . $dansetype->danseart . "', ";
  endforeach;
  $options .= ");";
    
         die(var_dump($options));
    
    
  ?&gt;
                
                
                
         &lt;?php echo form_dropdown('dansetype', $options, 'large');?&gt;

the $options now contain a string with

Code:
string 'array('1' => 'Folkedans', '2' => 'Selskabsdans', '3' => 'Linedans', );'   (length=70)

My question is, How do i fill my form_dropdown with information from my sql array??


#2

[eluser]srpurdy[/eluser]
Code:
$data = array();
    foreach ($dansetyper->result_array() as $row)
        {
        $data[$row['id']] = $row['danseart'];
        }

But you may also move ->result_array() into your model function.
#3

[eluser]Tenter[/eluser]

I have to send it
Code:
&lt;?php echo form_dropdown('dansetype', $options, 'large');?&gt;

so the $option is the array ?



But i got this
Code:
Fatal error: Call to a member function result_array() on a non-object
#4

[eluser]Tenter[/eluser]
how do i move it and where to place it ?

im pretty new to codeigniter.. so plz help Smile
#5

[eluser]srpurdy[/eluser]
Hi Tenter,

First change.
Code:
return $result->result();

to
Code:
return $result;

replace
Code:
<php
  $options = "";
  $options .= "array(";
  foreach($dansetyper as $dansetype):
        $options .= "'" . $dansetype->id . "' => '" . $dansetype->danseart . "', ";
  endforeach;
  $options .= ");";
    
         die(var_dump($options));
    
    
  ?&gt;

&lt;?php echo form_dropdown('dansetype', $options, 'large');?&gt;

with
Code:
$data = array();
    foreach ($dansetyper->result_array() as $row)
        {
        $data[$row['id']] = $row['danseart'];
        }  

&lt;?php echo form_dropdown('dansetype', $data, 'large');?&gt;

#6

[eluser]Tenter[/eluser]
It worked ! Ty m8 that was awesome Smile

my dropdown list is now filled with options from my array from the database Big Grin
#7

[eluser]srpurdy[/eluser]
Smile no problem glad I could help. Smile
#8

[eluser]AlanW[/eluser]
I have also achieved the drop down filled with options from my array from the database but when I use

$c_level = $this->input->post('c_level');

it comes back to my controller as an index integer, not the actual text that was in my drop down.

How do you fix this ?
#9

[eluser]CroNiX[/eluser]
Code:
$dropdown = array(
  'key' => 'option text'
)

When the form is sent, it is the 'key' that is sent. The 'option text' is what is displayed. The above would produce:

Code:
<option value="key">option text</option>

So if you want the text sent with the form, you need to put that as the 'key' in the above example, although I'm not sure why you wouldn't just send the key (id) like you currently are instead of the text.




Theme © iAndrew 2016 - Forum software by © MyBB