CodeIgniter Forums
Two almost identical methods but one failing - 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: Two almost identical methods but one failing (/thread-38597.html)



Two almost identical methods but one failing - El Forum - 02-12-2011

[eluser]edjon2000[/eluser]
Hello again

I am rewriting my web project to take into account CI Reactor and making a few design improvements on the way, however, I have run into a problem which for the life of me I cannot understand.

I will try to break it down as best as I can:-

Initially I created a method to generate values for a dropdown list and have since extended that to include some other pages on this project, so far, everything has worked out correctly, apart from this one variation( I do a lot of copying and pasting and just change the variables - as you will see shortly - Smile )

Here is the method that works correctly:-
Code:
// under development
    function create_vacancy_list()
    {
        $vacancies = array();
        $vacancy_values = NULL;
        $vacancy_keys = NULL;
        $this->db->select('name');
        $this->db->distinct();
        $this->db->order_by('name');
        $vacancies = $this->db->get('tbl_vacancies');
        if ($vacancies->num_rows() > 0)
        {
            foreach ($vacancies->result() as $vacancy)
            {
                $vacancy_keys[] = $vacancy->name;
                $vacancy_values[] = $vacancy->name;
            }
            $query = array_combine($vacancy_keys, $vacancy_values);
        }
//        echo '<pre>';
//        print_r($query);
//        echo '</pre>';
//        die('script stopped');
        return $query;
    }
and this is the one that does not work
Code:
// under development
    function create_client_list()
    {
        $clients = array();
        $client_values = NULL;
        $client_keys = NULL;
        $this->db->select('name');
        $this->db->distinct();
        $this->db->order_by('name');
        $clients = $this->db->get('tbl_clients');
        if ($clients->num_rows() > 0)
        {
            foreach ($clients->result() as $client)
            {
                $client_keys[] = $client->name;
                $client_values[] = $client->name;
            }
            $query = array_combine($client_keys, $client_values);
        }
As you can see, they are virtually identical but for some reason the "create_client_list method" does not work.

I could be missing something obvious and I apologise if I have, I have been trying to sort this out for the past four hours so I could be a bit "punch-drunk".

Anyway this is the error I am getting:-
Code:
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 324
So any advice about this would be greatly appreciated

Jon


Two almost identical methods but one failing - El Forum - 02-12-2011

[eluser]cahva[/eluser]
Does the former return anything as now it would definately give syntax error or did you just forgot to paste the whole method? Smile

Was it supposed be like this:
Code:
function create_client_list()
{
    $clients = array();
    $client_values = NULL;
    $client_keys = NULL;
    $this->db->select('name');
    $this->db->distinct();
    $this->db->order_by('name');
    $clients = $this->db->get('tbl_clients');
    $query = FALSE;
    
    if ($clients->num_rows() > 0)
    {
        foreach ($clients->result() as $client)
        {
            $client_keys[] = $client->name;
            $client_values[] = $client->name;
        }
        $query = array_combine($client_keys, $client_values);
    }
    return $query;
}



Two almost identical methods but one failing - El Forum - 02-12-2011

[eluser]edjon2000[/eluser]
Oh! (lots of really bad words you wouldn't want your mother to know that you knew), it was the final return $query statement that was missing)

Thank you cahva Smile

edit ... Actually I never thought about defining the $query variable as FALSE to start with although, it would make for better programming practices, so whilst it is not needed in this instance it is probably a good idea to initialise all variables before they are used, I generally do that with my $data arrays prior to building views, form input statements etc

What do you think ?

Jon