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

[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/p...l2+arrVal3

that codeigniter will convert the last parameter into an array.

Is it possible? Thanks.
#2

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

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

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

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

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

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

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

Cookies
#9

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

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




Theme © iAndrew 2016 - Forum software by © MyBB