CodeIgniter Forums
How to pass an array to a controller? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How to pass an array to a controller? (/showthread.php?tid=23752)

Pages: 1 2


How to pass an array to a controller? - El Forum - 10-21-2009

[eluser]gminuses[/eluser]
I'd like to pass an array as a parameter to my controller, the parameters number is unknown, but the last one is the array I want to pass in.

So how do I write the url?

I'm looking for something like:
http://localhost/mycontroller/myaction/param1/param2/.../arrVal1+arrVal2+arrVal3

that codeigniter will convert the last parameter into an array.

Is it possible? Thanks.


How to pass an array to a controller? - El Forum - 10-21-2009

[eluser]imn.codeartist[/eluser]
why don't you push into session instead of doing that... will have less headache if you do so :-)


How to pass an array to a controller? - El Forum - 10-21-2009

[eluser]gminuses[/eluser]
The request is made by front end, it has no idea of session.


How to pass an array to a controller? - El Forum - 10-21-2009

[eluser]rogierb[/eluser]
Serialize and urlencode it

Code:
$test = array(1,2,3,4);
$serialize = rawurlencode(serialize($test));

$url = site_url('some/url/'.var1.'/'.$serialize)

and in your controller

Code:
$array = unserialize(rawurldecode($the_serialized_array));


Not tested:-)


How to pass an array to a controller? - El Forum - 10-21-2009

[eluser]gminuses[/eluser]
This isn't SEO friendly, and if the client side is not generated by php(i.e, pure html + js), serialize & rawurlencode functions will have to be reimplemented. Anyway, thank you for the reply.


How to pass an array to a controller? - El Forum - 10-21-2009

[eluser]Boris Strahija[/eluser]
Why wouldn't you use sessions for this. For me this would be the most logical sollution.
A front end fo an application can also have sessions Wink

Other suggestions would be passing the array by form submission, or storing it into a database, but every user would then need some kind of a token, so that's actually a session Smile
You can also write the data into a txt file, but I don't think that's a good idea, because of security issues.

My suggestions is, use session. If possible with a database, because in this way you can store much more data into a session.
If you use a cookie the data size is limited.


How to pass an array to a controller? - El Forum - 10-21-2009

[eluser]gminuses[/eluser]
Could you tell me more about client side session and how to implement it?

If the values in the array is generated by javascript(client side), how do I pass these values to a controller as the last parameter, along with other values as the rest of paramters?

Using form submission will force it becoming a post request.


How to pass an array to a controller? - El Forum - 10-21-2009

[eluser]Boris Strahija[/eluser]
If you want to use it client-side you can use cookies.

Cookies


How to pass an array to a controller? - El Forum - 10-21-2009

[eluser]gminuses[/eluser]
Like you said, the cookie data size is limited, and the user might has disabled cookies.


How to pass an array to a controller? - El Forum - 10-21-2009

[eluser]gminuses[/eluser]
Deleted by myself.