Welcome Guest, Not a member yet? Register   Sign In
How to pass an array to a controller?

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:

that codeigniter will convert the last parameter into an array.

Is it possible? Thanks.

why don't you push into session instead of doing that... will have less headache if you do so :-)

The request is made by front end, it has no idea of session.

Serialize and urlencode it

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

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

and in your controller

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

Not tested:-)

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.

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

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.

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


Like you said, the cookie data size is limited, and the user might has disabled cookies.

Deleted by myself.

Theme © iAndrew 2016 - Forum software by © MyBB