Welcome Guest, Not a member yet? Register   Sign In
How to pass complex data to other controllers?
#1

[eluser]sauronlord[/eluser]
Hi,

I am new to Code Igniter, and I am curious how to pass a lot/complex data to another controller.

In particular, I have a table of values (think spreadsheet like).
I would like on a submit/save to gather all of the values for every row (there can be hundreds) and then process them in the same(or different controller). The rows may be selectable by checkboxes, and I may only want to send in the ones that are selected.

I read up on the URI segments, and all I could think of at the moment was to pass each row like this:

url.com/controller/myFunc/row1NameValue/row1PropertyXValue/row1PropertyYValue/...etc (repeat for row2...rowN)

And then to reconstruct the 2 dimensional array myself.

Is there an easy way to get from form elements into a Controller function?

I would prefer to probably use POST variables because of the size of data... is there way to retrieve the POST variables from a controller method?

Thank you
#2

[eluser]Udi[/eluser]
I hope I understood you, what is the problem to post the data to this other controller?

Maybe you can save this data in a cookie or session? maybe serialize it first?

what about saving it in the DB, in Temp table that you delete every hour or so... ?
#3

[eluser]sauronlord[/eluser]
[quote author="Udi" date="1259980870"]I hope I understood you, what is the problem to post the data to this other controller?

Maybe you can save this data in a cookie or session? maybe serialize it first?

what about saving it in the DB, in Temp table that you delete every hour or so... ?[/quote]

Hi,

I have thought of these ideas, and use them all over the place -- the problem is that they seem too low level.

I just basically want to store a NxM matrix and then retrieve it nicely.
I think I would prefer using the session over everything.

Is there a way to make objects persist between page transitions?

For instance, say I have an instance of object Foo and then I make a page transition, is there a way to get back this specific instance of Foo?
What about an array of Foo's?

Thanks very much
#4

[eluser]jedd[/eluser]
[quote author="sauronlord" date="1259972052"]
I am new to Code Igniter, and I am curious how to pass a lot/complex data to another controller.
[/quote]

Hi sauronlord. I think, from this question, that you perhaps need to re-think your application design. Do you mean from one controller here, to another controller? If so, there are probably more elegant ways to do what you're trying to do.

You certainly don't want to pass 'hundreds of items' through the URL, let alone try to reconstruct a 2-D array from that.

Post data might work, but it seems a little excessive. If it's a form you're submitting, then post data is obviously the right way of doing this - but if you just want to save state between sessions .. well .. there are other ways.

If your session data is kept in a cookie, you have 4KB (or less) to play with. If they're stored in a database, you have whatever the limit is on your default TEXT column size (assuming you used the code described in the Session page) for your database type.


Quote:Is there a way to make objects persist between page transitions?

serialize and store - ideally in the DB, as file system is likely to be slower, plus there's overhead when you start creating unique filenames.

I still think that your design needs a revisit if you find you need to recover a Foo object on every page load.
#5

[eluser]sauronlord[/eluser]
[quote author="jedd" date="1259993403"][quote author="sauronlord" date="1259972052"]
I am new to Code Igniter, and I am curious how to pass a lot/complex data to another controller.
[/quote]

Hi sauronlord. I think, from this question, that you perhaps need to re-think your application design. Do you mean from one controller here, to another controller? If so, there are probably more elegant ways to do what you're trying to do.

You certainly don't want to pass 'hundreds of items' through the URL, let alone try to reconstruct a 2-D array from that.

Post data might work, but it seems a little excessive. If it's a form you're submitting, then post data is obviously the right way of doing this - but if you just want to save state between sessions .. well .. there are other ways.

If your session data is kept in a cookie, you have 4KB (or less) to play with. If they're stored in a database, you have whatever the limit is on your default TEXT column size (assuming you used the code described in the Session page) for your database type.


Quote:Is there a way to make objects persist between page transitions?

serialize and store - ideally in the DB, as file system is likely to be slower, plus there's overhead when you start creating unique filenames.

I still think that your design needs a revisit if you find you need to recover a Foo object on every page load.[/quote]

Thanks for the advice.

I really just want an *easy* way of getting at an NxM (associative) matrix.
I use a DB to populate things, however I want to find out what changed in the previous spreadsheet and then do the update of the objects.


Can I get something like this: (pseudo code):


class MyForm extends Controller
{
$m_InitialDataGrid[][]; Associative array of rows mapping to another array(field => value)
$m_DataGrid[][]; // (Updated) Associative array of rows mapping to another array(field => value)

// Draw the HTML and populate the grid with default data from DB
public RenderScreen()
{
LoadGridFromDB($m_InitialDataGrid);
$m_DataGrid = $m_InitialDataGrid;
populateTemplate("myTemplate.tpl", $m_DataGrid);
}
// When the user clicks 'save' on the page
// I want to get at the updated values in $m_DataGrid;
public SavePressed()
{
// Loop through all elements in $m_DataGrid and compare each row to $m_InitialDataGrid
// And only commit to DB the elements that are different
}
};

Now let's say there is another method that will send the changed data to another page.
So I would like to populate some "datagrid object" and pass it over to the other page.
In particular, I do not want to have to deal with decoding the NxM array through POST vars directly, but would prefer some helper utilities to get this data across to another page.

Is it possible to serialize an object and save it to $_POST['MYOBJECT'];
And then retrieve it in another Controller like this:

if (isset($_POST['MYOBJECT']))
{
$dataGrid = new DataGrid($_POST['MYOBJECT']]);
}

Thanks for your help,
#6

[eluser]sauronlord[/eluser]
I can see that I can store userdata with the Session class.
Does this session class storing work for arbitrary datatypes (complex classes)?

Thank you
#7

[eluser]jedd[/eluser]
[quote author="sauronlord" date="1259996271"]I can see that I can store userdata with the Session class.
Does this session class storing work for arbitrary datatypes (complex classes)?
[/quote]

As I understand it, anything you can serialise will work.

But to be sure, and to answer your earlier questions, you should do some experimenting - it only involves a couple of lines of code, after all.




Theme © iAndrew 2016 - Forum software by © MyBB