CodeIgniter Forums

Full Version: How to pass an array to a controller?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[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.

El Forum

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

El Forum

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

El Forum

[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:-)

El Forum

[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.

El Forum

[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.

El Forum

[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.

El Forum

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

Cookies

El Forum

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

El Forum

[eluser]gminuses[/eluser]
Deleted by myself.
Pages: 1 2