Welcome Guest, Not a member yet? Register   Sign In
Controller function variables
#1

[eluser]Philo01[/eluser]
Hi!

I have been searching, but was not able to find the correct solution for this problem.

I'm using the Uri segments to load a user for example:

Quote:http://www.mydomain.com/users/details/5

Now in my controller I have the function "details".

Code:
function details($id){
    echo $id;
}

Works fine! But now when somebody leaves the id blank like so:

Quote:http://www.mydomain.com/users/details/

I get 1 error, because its missing:

Quote:Missing argument 1 for users::details()

What's the best way to tackle this problem?

Thanks! Smile
#2

[eluser]mi6crazyheart[/eluser]
How much i'm right i don't know but i feel, as u've design u'r controller function to accept any parameter... so u've to pass some variable else that will not work...
#3

[eluser]Georgi Budinov[/eluser]
Hello,

the best way is simply to add a default value for that $id variable like this:

Code:
function details($id = 0){
    echo $id;
}

Then you can add some logic to handle the situation of no id supplied.
#4

[eluser]Philo01[/eluser]
[quote author="Georgi Budinov" date="1277606913"]Hello,

the best way is simply to add a default value for that $id variable like this:

Code:
function details($id = 0){
    echo $id;
}

Then you can add some logic to handle the situation of no id supplied.[/quote]

Thanks! Smile




Theme © iAndrew 2016 - Forum software by © MyBB