0

我有一个功能如下:

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;
    }
}

我正在尝试从输出到模板中访问这些数据,如下所示:

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

然而,这并没有显示任何东西。我觉得这是一件小事,我应该知道得更好。当我在函数内的任一数组上调整 print_r($arrayname) 时,我得到了可疑的数组值。当我 print_r OUTSIDE 函数时,它什么也不返回。

4

1 回答 1

0

你的函数没有返回任何东西。添加如下所示的返回。

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,
     ); 
}

您没有将return函数的值设置为变量

$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
     );
于 2013-07-16T20:35:39.977 回答