Welcome Guest, Not a member yet? Register   Sign In
URIs and optional parameters
#1

[eluser]adrian westlake[/eluser]
Hi,

Just starting to write my first commercial application in CI, but have a questions regarding the segment url format as opposed to the traditional query string.

What if I have a function with two optional parameters. In other words each one has a default value if not passed.

Code:
function myFunction($param1 = "default", $param2 = "default"){

}

With the traditional GET method it is fine as it associates the keys and values.

But with the segment method if I passed

Code:
www.mydomain.com/controller/function/param

How would I know if the param was for the first or second optional parameter. In other words I have to pass all parameters for a function to ensure I know which is which. Does that make sense???
#2

[eluser]esra[/eluser]
Read through all of the user_guide content including 'uri' in the table of contents titles with specific reference to learning how uri segments work in CI. CodeIgniter allows you to remap the URI handler using the routes.php config file or directly from your controllers.
#3

[eluser]kechik3000[/eluser]
Read through the user guide. Thanx for suggesting reading it ALL through because I found nothing to help the problem above.
#4

[eluser]Henrik Pejer[/eluser]
I believe that this page: http://ellislab.com/codeigniter/user-gui.../urls.html will be helpful.
#5

[eluser]kechik3000[/eluser]
But what if I don't want to use the traditional GET method? I still like the uri segment as parameters because they're neat. Only prob is that it doesn't understant optional parameter unless if I put a dummy parameter, which is not so neat...
#6

[eluser]Henrik Pejer[/eluser]
What do you mean?

does this not work for you:

http://www.fakeshop.com/list/products/boats

Where you have a controller call 'list', a function called 'products' and the parameter for the function would be 'boats'.

If I'm not understanding you correctly: please give me some more examples!
#7

[eluser]Henrik Pejer[/eluser]
Check out this page:

http://ellislab.com/codeigniter/user-gui...llers.html

look for Passing URI Segments to your Functions.
#8

[eluser]kechik3000[/eluser]
Um, ok.. It's like this...
Let's say I have a controller 'List', and within that a function 'Products' but this time, 'Products' receive 2 or more parameters which are all optional
Code:
function Products($x = NULL, $y = NULL, $z = NULL){
// some thing
}
..products/x/y/z

suppose I only intended to pass 2 out of those 3 parameter, using uri segment, how does the function knows which one is $x, $y or $z?

can I put something like ..products/44/NULL/77 ?
#9

[eluser]Henrik Pejer[/eluser]
They will be passed in the order they appear in the URI.

So

..products/boats/big/red

would mean that x=boats, y=big and z=red.

If the URI would be:

..products/red/big/boats

then x=red, y=big and z=boats

The order in the URI is order that they will be parameters to the function.

If you want to be able to send either x, y or z to the function then you would either, as you said, have ..products/44/null/77 and then you'd have to test for value of 'null', or you go here, http://ellislab.com/codeigniter/user-gui...s/uri.html and look for $this->uri->uri_to_assoc.

That would be the best way since then you'd make your URI like this:

..products/x/boats/y/red/z/big

With that function, $this->uri->uri_to_assoc, you can also set default keys allowing you to get 'false' when that is not present in the URI.

Read through the manual again: it really helps! I've read it several times before I started using CI since most of the basic answers are in there. There is a PDF-version available ( http://ellislab.com/forums/viewthread/49100/ ) and even though its not the latest version, you'd get the basic 95%.

I hope I haven't confused you to much Wink
#10

[eluser]sempsteen[/eluser]
Quote:can I put something like ..products/44/NULL/77 ?

You may use this but in a different manner. For this example php will not assign the y variable to null (special type) but it will assign it as a string. So you may write your function like:

Code:
function Products($x = 'NULL', $y = 'NULL', $z = 'NULL') {
    if ($x != 'NULL') {
        ...
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB