CodeIgniter Forums
Problem with Select Options/ arrays - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Problem with Select Options/ arrays (/thread-21248.html)



Problem with Select Options/ arrays - El Forum - 08-04-2009

[eluser]eric cumbee[/eluser]
i am working on a facebook app and am trying to generate a dropdown select box with FBML. i have built my model function to get the state id and name from the database and drop them into an array. i call that function from the controller and pass that data to the view. when i try i echoing out the resulting array as a parameter of echo form_dropdown() it works fine. but when i try building option statements for the fbml select box or i try echoing out the data in a foreach(as seen in the view code posted below) all i get the first letter of each state name repeated twice (the output is at the bottom). i have been trying to figure out what i am doing wrong all afternoon. i am hoping a fresh set of eyes might help.
Model Function
Code:
function Get_all_states()
        {
           $states = array();
           $this->db->select('id,name');
            $query = $this->db->get('state');
            if ($query->num_rows() > 0)
            {
                foreach ($query->result() as $row)
                {
                    $states[$row->id] = $row->name;
                }
            }
            return $states;
        }

controller function
Code:
function select_state()
    {
        $this->load->helper('form');
        $this->load->model('geo');
        $data['states'] = $this->geo->Get_all_states();
       $this->load->view('/voters/selectstate',$data);
    }
View Code
Code:
<?php
              foreach($states as $rows)
              {
                echo $rows['id'];
                echo $rows['name'];
                echo '</br>';
              }
    ?&gt;

output
Code:
AA
AA
AA
AA
CC
CC
CC
DD
DD
FF
GG
HH
II
II
II
II
KK
KK
LL
MM
MM
MM
MM
MM
MM
MM
MM
NN
NN
NN
NN
NN
NN
NN
NN
OO
OO
OO
PP
RR
SS
SS
TT
TT
UU
VV
VV
WW
WW
WW
WW
AA
MM
GG
MM
NN
PP
PP
VV
AA
AA
AA



Problem with Select Options/ arrays - El Forum - 08-04-2009

[eluser]ecigraeme[/eluser]
Do you need the processing step in your Get_all_states function

I would try changing it to this

Code:
function Get_all_states()
{
    $states = array();
    $this->db->select('id,name');
    $query = $this->db->get('state');
    return $states;
}



Problem with Select Options/ arrays - El Forum - 08-04-2009

[eluser]eric cumbee[/eluser]
[quote author="ecigraeme" date="1249439444"]Do you need the processing step in your Get_all_states function

I would try changing it to this

Code:
function Get_all_states()
{
    $states = array();
    $this->db->select('id,name');
    $query = $this->db->get('state');
    return $states;
}
[/quote]

that would declare the $states array and return it without putting any results in it.


Problem with Select Options/ arrays - El Forum - 08-05-2009

[eluser]ecigraeme[/eluser]
oops, maybe

return $query->result_array();


Problem with Select Options/ arrays - El Forum - 08-05-2009

[eluser]eric cumbee[/eluser]
ive tried that, i even did a var_dump on the return and it has the correct values.

also i have used this same basic model fucntion dozens of times before and never had this problem. that code comes straight from the packt codeigniter book.