Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a function as follow:

function get_employee_information() 
{
$this->db
        ->select('id, name');
$query = $this->db->get('sales_people');

$employee_names = array();
$employee_ids = array();

foreach ($query->result() as $row) {
    $employee_names[$row->id] = $row->name;
    $employee_ids[] = $row->id;
    }
}

I'm trying to access this data from within an output to template, like this:

$this->get_employee_information();    
$output = $this->template->write_view('main', 'records', array(
            'employee_names' => $employee_names,
            'employee_ids' => $employee_ids,
            ), false);

Yet this isn't displaying anything. I feel like this is something small and I should know better. When I tun print_r($arrayname) on either array WITHIN the function, I get the suspected array values. When I print_r OUTSIDE of the function, it returns nothing.

share|improve this question
    
your function is not returning anything.. Add return array('employee_names'=>$employee_names, 'employee_ids'=>$employee_ids); at the end of your funtion. –  user20232359723568423357842364 Jul 16 '13 at 20:33

1 Answer 1

Your function is not returning anything. Add the return shown below.

function get_employee_information() 
{
    $this->db->select('id, name');
    $query = $this->db->get('sales_people');

    $employee_names = array();
    $employee_ids = array();

    foreach ($query->result() as $row) {
        $employee_names[$row->id] = $row->name;
        $employee_ids[] = $row->id;
     }

    return array(
        'employee_names'=>$employee_names, 
        'employee_ids'=>$employee_ids,
     ); 
}

You are not setting the return value of the function to a variable

$employee_info = $this->get_employee_information();    
$output = 
    $this->template->write_view(
        'main', 'records', 
        array(
            'employee_names' => $employee_info['employee_names'],
            'employee_ids' => $employee_info['employee_ids'],
         ), 
         false
     );
share|improve this answer
    
Ahh, thank you. I had tried return $employee_names; and return $employee_ids; but not having them return as an array. I see. Thank you! –  PrinceEnder Jul 16 '13 at 20:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.