Welcome Guest, Not a member yet? Register   Sign In
Sharing data between multiple calls to controller
#1

[eluser]jmolly[/eluser]
Hello.

I'm writing a little application that exports data from a cvs file to the database.
I need to ask the user through a multiple checkboxs screen what are the categories he wants to store on DB.
I'm using only one controller.
From my main controller I call the view with the checkboxs, and the screen submits back to another function of the controller. Next I have to do the same but for the subcategories. When categories and subcategories are selected, I call a function on my controller that needs to check what categories and subcategories should I store on DB.

I don't know if I'm facing an erroneous design, but I can't find a way to share the categories and subcategories values between my functions calls in the CodeIgniter documentation, because the Controller is instantiated each time it's invoked.

Is correct to use session or cache to do this? I need about 40.000 registers.

Sorry for my poor English and thanks in advance!



Code:
class Controller extends CI_Controller{
  var $categories = array();
  var $subcategories = array();

function select_categories(){
   //Here I show the categories_view
}

function get_categories(){
   //Method called from the categories_view
   //Here I store the subcategories into a class variable
   $this->categories = "Post data"
}

function select_subcategories(){
   //Here I show the view
  
}

function get_subcategories(){
   //Method called from the subcategories_view
   //Here I store the subcategories into a class variable
    $this->subcategories = "Post data"
}

function dump_to_db(){
      //Check contents of $this->categories and $this->subcategories but are empty ;(
}

}
#2

[eluser]TWP Marketing[/eluser]
Why not make a single view which collects both category and sub-category in the same view?
I think this is the simpler way.

Or you could pass the selected/posted category to the sub-category function as a parameter?
Code:
function select_subcategories($category)
{
$subcategories = $this->get_subcategories($category);

$this->load_view('subcategory_select',$subcategories);
}

I also think you are calling a method from within a view (if I understand your statment). This works but it is not good practice according to MCV design. If I'm not reading your statement correctly, no problem.
#3

[eluser]jmolly[/eluser]
Your solution appears to be ok, but I think it's harder to write code for that than using class vars (if CI Controller were a singleton). And the html code should be dynamic (if you select one category, then all of their subcategories should be expanded) Imagine ten or more linked levels...

But what about an online shop where the cart data should be accessible until is destroyed? Where should I store that data?
#4

[eluser]InsiteFX[/eluser]
MY_Controller ?

InsiteFX
#5

[eluser]jmolly[/eluser]
I've readed MY documentation but I can't understand why the base class
won't be re-instantiated each time (when extended class will).
Finally I set the session-store option to database and use session to store my
objects between multiple calls to the Controller.

While I read the csv file I create Model objects calling its
constructors, but I think that is not the "CI way" to do that, because the
load-Model option exists for any reason Tongue

It's hard to change your mind when you are an OO programmer and begins to
use CI (I guess is focused in string arrays nor object arrays).

Anyway, the framework rocks!




Theme © iAndrew 2016 - Forum software by © MyBB