• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass a variable between a controller

#1
[eluser]Taff[/eluser]
Is it possible (or advisable) to pass variables between controllers?

I have one controller which I am using to upload a file to the server.
After it has been uploaded I would like that variable accessible via my parse_csv controller so this is roughly what I have:

Code:
class Katalog extends Controller {

    function Katalog(){
        parent::Controller();    
        $this->csv_file;
    }
    function upload_csv(){
        $this->load->library('Upload');
        if ($this->upload->uploaded) {
        //Save $this->csv_file for future reference somewhere
        $this->csv_file=$this->upload->file_name;
        //load view with link to parse_csv
        }
    }
    function parse_csv(){
        $this->load->library('Csvparser');
        //grab the data we saved in upload_csv
        $file_name=$this->csv_file;
        $this->csvparser->parse($file_name);
    }
}

Sorry for the silly question, I can't find anything in the forum about it which is probably down to me being the only one who doesn't know how to do it.

Thanks for any help!
Taff

#2
[eluser]Adam Griffiths[/eluser]
I'm not quite sure if this is what you're after, but maybe you can use flashdata? In the session library.

#3
[eluser]Jilani Jidni[/eluser]
[quote author="Taff" date="1217346497"]

Code:
class Katalog extends Controller {

    function Katalog(){
        parent::Controller();    
        $this->csv_file;
    }
    function upload_csv(){
        $this->load->library('Upload');
        if ($this->upload->uploaded) {
        //Save $this->csv_file for future reference somewhere
        $this->csv_file=$this->upload->file_name;
        //load view with link to parse_csv
        }
    }
    function parse_csv(){
        $this->load->library('Csvparser');
        //grab the data we saved in upload_csv
        $file_name=$this->csv_file;
        $this->csvparser->parse($file_name);
    }
}
[/quote]

its not clear when you call this parse_csv. if you call this function at the end of upload_csv then try this

Code:
class Katalog extends Controller {

    var $csv_file;

    function Katalog(){
        parent::Controller();    
    }

    rest of the code here
    
}

otherwise use session.

#4
[eluser]Taff[/eluser]
Thank you both for your answers.

I had already tried both of the examples you mentioned.

I was hoping there was a way to avoid using sessions.

Taff

#5
[eluser]Tom Glover[/eluser]
You could load the controllers in other controllers by using Wick (Controller Loader).

#6
[eluser]Pascal Kriete[/eluser]
Wacky, the function is in the same controller - no need for [strike]ugly[/strike] alternative solutions.

The easiest way to do this, is to just call that function after you upload and a) pass the data as a parameter, or b) put it in a class variable (as you're doing now).
Code:
$this->parse_csv($data);

If you're going to display a link that the user has to click - that's two requests, you need to either store the data in a file, db, or the session.
If you're using CI sessions, putting it into the session cookie would be mighty redundant, since the user just uploaded the file to the server. FWIW, I would ask if they want to parse it in the upload form, instead of showing a link as an afterthought.

#7
[eluser]Taff[/eluser]
Thank you all for your replies.

The resaon I wanted it to be a link is that the entire process involves a lot of links, and I was worried about getting timed out, but maybe simply passing the data as a parameter is the best solution.

I think I am missing something...the code that I'm using in my original post

$this->csv_file

Is empty when I try to target it in parse_csv even though I am giving it a value in upload_csv...is this correct?

Thanks again,
Taff

#8
[eluser]Tom Glover[/eluser]
[quote author="inparo" date="1217384452"]Wacky, the function is in the same controller - no need for [strike]ugly[/strike] alternative solutions.

The easiest way to do this, is to just call that function after you upload and a) pass the data as a parameter, or b) put it in a class variable (as you're doing now).
Code:
$this->parse_csv($data);
[/quote]

Thanks for the advice!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.