我有codeigniter的控制器功能,
public function add_attachments($openid)
{
$config = array(
'upload_path' => './uploads/attachments/',
'allowed_types' => 'gif|jpg|png|jpeg|doc|pdf',
'max_size' => '1024000000',
'multi' => 'all'
);
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = $this->upload->data();
}
$new_array = array_column ($data, 'full_path');
}
我可以获得$ new_array的输出,
但是我想在另一个函数中使用这个输出,这个函数正好在同一个类中,
例如,
public function getback()
{
print_r($new_array);
}
简而言之,一旦执行了1个函数,一些值存储在变量中,需要在另一个函数中使用,
我该怎么做?
谢谢,
答案 0 :(得分:1)
只需将该函数中的值传递为
即可public function add_attachments($openid) {
$config = array(
'upload_path' => './uploads/attachments/',
'allowed_types' => 'gif|jpg|png|jpeg|doc|pdf',
'max_size' => '1024000000',
'multi' => 'all'
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
} else {
$data = $this->upload->data();
}
$new_array = array_column($data, 'full_path');
$this->getback($new_array);
}
public function getback($new_array = array()) {
print_r($new_array);
}
<强>被修改强>
使用js传递数据
public function add_attachments($openid) {
//your code...
echo json_encode($new_array);exit;
}
现在在你的ajax成功函数中
success:function(data){
var data = $.parseJSON(data)
//send it to your another function
}