• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An alternative way to send variables to functions?

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

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

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

#4
[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?

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

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

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

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

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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.