【问题标题】:How to get value from one function to another function?如何从一个函数获取价值到另一个函数?
【发布时间】:2014-10-16 10:32:27
【问题描述】:

我必须在一个函数是回调函数的地方工作。现在我想将值(来自“$iamge_location”变量的值)获取到另一个函数中。我正在尝试从回调函数返回值并将其打印到另一个调用回调函数的函数中。但我没有得到价值请帮助..

这是我的控制器代码:

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

【问题讨论】:

  • 将变量设为全局变量并使用它。

标签: php codeigniter codeigniter-2


【解决方案1】:

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

改变

return $image_location;

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2020-09-26
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多