如何在Codeigniter中的同一个控制器中重用功能?

时间:2018-03-26 03:38:41

标签: php codeigniter

public function reuse_user(){

  //get data from table arrival to display
    $data_user['fields'] = array(
         //display feilds
      'id',
      'user_name',
      'user_email',
      'user_psw',
      'user_cpsw',
      'user_designation',
      'usertype_id',
      'user_image'    
    );

    $data_user['order'] = 'id asc';

    //process get data from db
    $data['result_users'] = $this->mod_users->get_users($data_user);

   // var_dump($data['result_users']); die();
}


public function new_appointment(){

    $this->reuse_user();

    $data['related_view']='system_new_appointment';
    $this->load->view('template', $data);

}

我在视图页面(system_new_appointment.php)上的reuse_user()函数数据未显示

我能知道正确的方法吗?

1 个答案:

答案 0 :(得分:2)

reuse_user需要返回$data数组,并且需要将返回的数组分配给新函数中的数组...函数不知道其他函数中存在哪些变量。

public function reuse_user(){

  //get data from table arrival to display
    $data_user['fields'] = array(
         //display feilds
      'id',
      'user_name',
      'user_email',
      'user_psw',
      'user_cpsw',
      'user_designation',
      'usertype_id',
      'user_image'    
    );

    $data_user['order'] = 'id asc';

    //process get data from db
    $data['result_users'] = $this->mod_users->get_users($data_user);

   // var_dump($data['result_users']); die();

   return $data;
}


public function new_appointment(){

    $data = $this->reuse_user();

    $data['related_view']='system_new_appointment';
    $this->load->view('template', $data);
}

或者将数组分配给类范围:

public function reuse_user(){

  //get data from table arrival to display
    $data_user['fields'] = array(
         //display feilds
      'id',
      'user_name',
      'user_email',
      'user_psw',
      'user_cpsw',
      'user_designation',
      'usertype_id',
      'user_image'    
    );

    $data_user['order'] = 'id asc';

    //process get data from db
    $this->data['result_users'] = $this->mod_users->get_users($data_user);

   // var_dump($data['result_users']); die();
}


public function new_appointment(){

    $this->reuse_user();

    $this->data['related_view']='system_new_appointment';
    $this->load->view('template', $this->data);
}