如何从一个函数到另一个函数获取值?

时间:2014-10-16 10:32:28

标签: php codeigniter codeigniter-2

我必须在一个函数是call_back函数的地方运行。现在我希望将值(来自" $ iamge_location"变量的值)转换为另一个函数。我试图从回调函数返回值并将其打印到调用回调函数的另一个函数中。但我没有得到价值PLZ的帮助..

这是我的控制器代码:

    function RoomImageAdd(){
        $this->form_validation->set_rules('roomImage', 'Upload Image', 'callback_upload_Image');

        $roomid = $this->input->post('roomid');

        if ($this->form_validation->run() == FALSE) {
            $this->addRoomImage($roomid);
        }
        else {
            echo $image_location;
        }
    }

    function upload_Image(){

        if(!empty($_FILES['roomImage']['name']))
        {
            $filename = date('YmdHis');

            $config['upload_path'] = 'assets/img/upload/rooms/';
            $config['allowed_types'] = 'jpg|jpeg';
            $config['file_name'] = $filename;
            $config['max_size'] = '2000';
            $config['remove_spaces'] = true;
            $config['overwrite'] = false;

            $this->upload->initialize($config);

            if (!$this->upload->do_upload('roomImage'))
            {
                $this->form_validation->set_message('upload_Image', $this->upload->display_errors('',''));
                return FALSE;
            }
            else
            {
                $image_data = $this->upload->data();

                $image_location = 'assets/img/upload/rooms/'.$image_data['file_name'];

                $config['image_library'] = 'gd2';
                $config['source_image'] = $image_data['full_path'];
                $config['new_image'] = $image_data['file_path'].'room_thumb/';
                $config['maintain_ratio'] = FALSE;
                $config['create_thumb'] = TRUE;
                $config['thumb_marker'] = '_thumb';
                $config['width'] = 280;
                $config['height'] = 280;

                $this->image_lib->initialize($config);

                if (!$this->image_lib->resize()) {

                    $error = array('error' => $this->image_lib->display_errors());

                } else {

                    $this->image_lib->resize();
                }
                return $image_location;
            }
        }
        else{
            $this->form_validation->set_message('upload_Image', "Unexpected Error! No File Selected!");
            return FALSE;
        }
    }       

1 个答案:

答案 0 :(得分:1)

在您的控制器中,您应该能够添加一个字段变量image_location,您可以在update_Image函数中设置该字段变量。{/ p>

更改

return $image_location;

$this->image_location = $image_location;
return true; // because a boolean is expected off of a validation callback

现在,您可以使用$this->image_location访问RoomImageAdd方法中的位置。