Welcome Guest, Not a member yet? Register   Sign In
whats the best way for an uploaded image or upload failure message to stay on the same screen when i refresh?
#1

[eluser]James_B[/eluser]
I have tried putting my upload_form, upload_failure and upload_success view files all in my index part of my controller but with no success.

Here is my controller:
Code:
<?php
class HomeProfile extends CI_Controller
{


  function HomeProfile()
  {
    parent::__construct();
    $this->load->model("profiles");
    $this->load->model("profileimages");
    $this->load->helper(array('form', 'url'));
  }

  
  function upload()
  {
   $config['upload_path'] = './web-project-jb/assets/uploads/';
   $config['allowed_types'] = 'gif|jpg|png';
   $config['max_size'] = '10000';
   $config['max_width'] = '1024';
   $config['max_height'] = '768';
  
   $this->load->library('upload', $config);
   $img = $this->session->userdata('img');
   $username = $this->session->userdata('username');
   $this->profileimages->putProfileImage($username, $this->input->post("profileimage"));
   //fail show upload form
   if (! $this->upload->do_upload())
   {
  
    $error = array('error'=>$this->upload->display_errors());
    
    $username = $this->session->userdata('username');
    
    
    $viewData['username'] = $username;
    $viewData['profileText'] = $this->profiles->getProfileText($username);
    
    $this->load->view('shared/header');
    $this->load->view('homeprofile/homeprofiletitle', $viewData);
    $this->load->view('shared/nav');
    $this->load->view('homeprofile/upload_fail', $error);
    $this->load->view('homeprofile/homeprofileview', $viewData, array('error' => ' ' ));
    $this->load->view('shared/footer');
  
   }
  
   else
   {
    //successful upload so save to database
    
    
    $file_data = $this->upload->data();
    
    
    $data['img'] = base_url().'./web-project-jb/assets/uploads/'.$file_data['file_name'];
    // you may want to delete the image from the server after saving it to db
    // check to make sure $data['full_path'] is a valid path
    // get upload_sucess.php from link above
    //$image = chunk_split( base64_encode( file_get_contents( $data['file_name'] ) ) );
    

    
    $this->username = $this->session->userdata('username');
  
    $data['profileimages'] = $this->profileimages->getProfileImage($username);
    
    
    $viewData['username'] = $username;
    $viewData['profileText'] = $this->profiles->getProfileText($username);
    
    $username = $this->session->userdata('username');
    
    $this->load->view('shared/header');
    $this->load->view('homeprofile/homeprofiletitle', $viewData);
    $this->load->view('shared/nav');
    $this->load->view('homeprofile/upload_success', $data);
    $this->load->view('homeprofile/homeprofileview', $viewData, array('error' => ' ' ));
    $this->load->view('shared/footer');
   }
  
  }
  

  
  function index()
  {
  
   $username = $this->session->userdata('username');
  
   $data['profileimages'] = $this->profileimages->getProfileImage($username);
    
   $viewData['username'] = $username;
   $viewData['profileText'] = $this->profiles->getProfileText($username);
    
   $this->load->view('shared/header');
   $this->load->view('homeprofile/homeprofiletitle', $viewData);
   $this->load->view('shared/nav');
   $this->load->view('homeprofile/upload_form', $data);
   $this->load->view('homeprofile/homeprofileview', $viewData, array('error' => ' ' ) );
   $this->load->view('shared/footer');
  }

}

my view files - is it correct to have these separate or should they be together in the one file?

I know I should be really using redirect('homeprofile/index'); in the upload if/else st
atements but since Im having these issues I need to bascially load the index view lines into both parts of upload()
#2

[eluser]adamck[/eluser]
You can save the Upload success or upload fail to a flashdata session and then do $this->index();

This will run the index() function after you have set the flashdata.

Then in your view, you can have a section to see if the flashdata is not empty and display the flashdata contents.

Code:
if (! $this->upload->do_upload())
   {
      $this->session->set_flashdata('error', $this->upload->display_errors());
      $this->index();
   }
else
   {
      $this->session->set_flashdata('success', 'File Uploaded');
      // ADD TO DB HERE
      $this->index();      
   }

When your index loads the view, the flashdata will exist for that 1 instance, when you refresh it will have gone.
Code:
<?php if(!empty($this->session->flashdata('success')
{
    echo $this->session->flashdata('success');
}
else if(!empty($this->session->flashdata('error')
{
    echo $this->session->flashdata('error');
}
?>

Also... you dont need to pass the username to the view in order to display it, just put the code directly into your view...
Code:
<?php echo $this->session->userdata('username'); ?>




Theme © iAndrew 2016 - Forum software by © MyBB