I am creatting a Code Igniter project in which I want to pass a variable through the URL like a get statement, like this:
url: /site/cake/1
controller function: cake($var)
but when the variable is left blank, I receive an error, how can I get code igniter, to ignore this?
views:
652answers:
2
+6
A:
In your controller, do this:
function cake($var = null) {
// your other code here
}
When $var
isn't present in the URL, it will be set to null
and you'll receive no error.
Colin
2010-01-31 22:50:16
it works!:DThanks
jpoles1
2010-01-31 23:23:13
Great - I'm glad :) If my answer helped, would you mind accepting it as the correct answer?
Colin
2010-01-31 23:29:30
You should be setting it to `null` instead of `false`, as `false` is an actual valid value.
Chetan
2010-05-28 10:06:05
@Chetan: You're absolutely right. I've edited my answer. Thanks for pointing that out.
Colin
2010-10-26 03:06:33
@Colin: Kinda late there :)
Chetan
2010-10-26 03:28:43
A:
To explain why Colin's answer works: The issue you had, was that there was no default value for that controller function. In php, creating a default value for a function parameter is done by assigning it a value in the function definition ($var = false). Now when the cake() function is called with no parameter, it will set $var to false by default.
ocdcoder
2010-02-02 23:20:02