2014-10-16 222 views
1

我必須在一個函數是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; 
     } 
    }  
+0

使全局變量和使用它。 – shafiq 2014-10-16 10:38:05

回答

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_locationRoomImageAdd方法來訪問的位置。

+0

太棒了,它完美的作品。謝謝。 – 2014-10-16 11:23:36

相關問題