Welcome Guest, Not a member yet? Register   Sign In
[RESOLVED] 'Array to string conversion' error when using query to obtain dropdown list values from database.
#1

[eluser]Tim in Tulsa[/eluser]
Hello, all! I'm new to CodeIgniter and hope you can give me some suggestions on this problem.

In my model, I have a function called getDomainList that simply takes a table name and retrieves the database keys and values from a domain table (table with lookup values) and assigns them to an array of key-value pairs. The function works fine and a print_r() statement in the function shows appropriate values before returning the array. For example:
Code:
Array ( [1] => Preschool [2] => Ages 1 - 3 [3] => Ages 4 - 5 [4] => Kindergarten [5] => 1st - 2nd Grade [6] => 3rd - 4th Grade [23] => 5th Grade [8] => Junior High/Middle School [9] => High School [10] => Adult )

However, when I call this function from my controller to assign the list of values to my $data array (e.g., $data['ages_grades_list'] = $this->workroom_model->getDomainList('ages_grades');), I get the error:
Code:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: controllers/workroom.php
Line Number: 76

I can't figure out why this is happening and I don't understand why PHP would be trying to do an implicit cast to a string.

Thanks for any help/advice that you can provide.

Tim
#2

[eluser]adamfairholm[/eluser]
Hey Tim,

Could we see the getDomainList() function?

Adam
#3

[eluser]Tim in Tulsa[/eluser]
Thanks, Adam!

Yes... Here's the getDomainList() function in the model:

Code:
function getDomainList($table_name = '')
        {
            $this->load->database();
            if(!empty($table_name))
            {
                $table_fields = $this->db->list_fields($table_name);
                
                $query = $this->db->get($table_name);
                
                if($query->num_rows() > 0)
                {
                    $domain_list = array();
                    foreach($query->result() as $row)
                    {
                        $keys[] = $row->$table_fields[0];
                        $values[] = $row->$table_fields[1];
                    }
                    
                    $domain_list = array_combine($keys, $values);
                }
                
                print_r($domain_list);
            }
            
            return $domain_list;
        }

As I mentioned in the original post, print_r($domain_list) is just here for debugging purposes (so that I can see that the array is properly formed).

Thanks for any suggestions/ideas. If there's a better way to do this, I'd appreciate your recommendation.

Tim
#4

[eluser]Tim in Tulsa[/eluser]
OK... I figured this out. The "issue" with the getDomainList() function turned out to be a red herring.

It turns out that the problem was in my controller implementation. I used .= to try to "add" information to the $data array. Obviously, this caused the implicit cast of $data from an array to a string. Here's the code that I had:
Code:
$data = $this->workroom_model->commonLayout();

            $data .= $this->workroom_model->getInputFormFieldAttributes();

I corrected the code as follows and voila everything fine:
Code:
$data = $this->workroom_model->commonLayout();

            $data = array_merge($data, $this->workroom_model->getInputFormFieldAttributes());

I guess this is just another newbie mistake! :-)

Tim




Theme © iAndrew 2016 - Forum software by © MyBB