CodeIgniter Forums
An alternative way to send variables to functions? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: An alternative way to send variables to functions? (/showthread.php?tid=7222)

Pages: 1 2


An alternative way to send variables to functions? - El Forum - 03-30-2008

[eluser]KeyStroke[/eluser]
I read in the guide that you can send variables this way:
http://example.com/controller/variable1/variable2

However, in my case, my variables aren't always all needed at the same time, so I'd rather use something like:
http://example.com/controller/?var1=this&var2=that

because it's more flexible.

Is this possible in CodeIgniter? I just need an alternative that allows me to control variables like that.


Your help is much appreciated Smile


An alternative way to send variables to functions? - El Forum - 03-30-2008

[eluser]Pascal Kriete[/eluser]
By default CI doesn't allow query strings, and while you can turn them on, you lose the nice /controller/function structure when you do.

One thing to look into would be the uri_to_assoc function of the uri class.


An alternative way to send variables to functions? - El Forum - 03-30-2008

[eluser]KeyStroke[/eluser]
Is there any other way around it to control variables? because if I have for example 12 variables, and would like to use only 2 of them on a specific page, then the method you've mentioned would make it look really messy.


An alternative way to send variables to functions? - El Forum - 03-30-2008

[eluser]adamp1[/eluser]
Other than those two ways, I don't think there is another way. Other than storing them in a session variable. Personally the uri_to_assoc function is your best bet. Keeps the url nice and clean then.

I don't get how using the ?variable=value method wouldn't make it messy if you had to always pass 12 on?


An alternative way to send variables to functions? - El Forum - 03-30-2008

[eluser]Jamie Rumbelow[/eluser]
You can pass variables on in arrays to views and controllers, so you just make an array of all the variables you want, give it to the function as an argument, and use it in the function itself. Easy.


An alternative way to send variables to functions? - El Forum - 03-30-2008

[eluser]adamp1[/eluser]
You can't do that if your trying to pass the data through the url.


An alternative way to send variables to functions? - El Forum - 03-30-2008

[eluser]Pascal Kriete[/eluser]
Last time I checked uri_to_assoc returns an array. In fact, it's pretty much equivalent to having a $_GET array at that point. I don't see how it's different. 12 is messy no matter how you do it.


An alternative way to send variables to functions? - El Forum - 03-30-2008

[eluser]Jamie Rumbelow[/eluser]
Well, If you keep it in an array you can only call on the variables you need.


An alternative way to send variables to functions? - El Forum - 03-30-2008

[eluser]adamp1[/eluser]
@Jemgames: But how are you passing that array to the controller? You can't just 'pass' it, it has to be through the url or a stored cookie/session array.


An alternative way to send variables to functions? - El Forum - 03-30-2008

[eluser]Jamie Rumbelow[/eluser]
When you load the controller, via $this->load->controller("news",$array);

Or, if you are calling the controller from the URI, use a hook, then pass it there.