Welcome Guest, Not a member yet? Register   Sign In
URL Parameter passing
#1

[eluser]vindhyareddy[/eluser]
I am developing a RESTful web services API.

I am trying to run the url :
http://localhost/devit/users/test/12/user/123

Code:
function test_get()
    {
        $user_type = $this->uri->segment(3, 'default value');
        $user_id = $this->get('user');
        echo $user_type;
        echo $user_id;
    }

In the url , devit is my folder
users is my controller name
test if the function name
user is a parameter
12 is a param value..without any param name..
123 is a parameter value of the parameter user.

When I try running this, I am not able to get the value of $user_id
However I am able to get the value of $user_type.

Cant I give any other parameter names after a parameter without a param name?

Hope to get a reply asap!
#2

[eluser]Lyon[/eluser]
I don't think this can be done in CodeIgniter.
As I understand it each value separated by a slash is a parameter value. They do not have names.
In your example you would want :
Code:
function test_get()
{
    $user_type = $this->uri->segment(3, 'default value');
    $user_id = $this->uri->segment(5, 'default_value');
    echo $user_type;
    echo $user_id;
}
Or most likely you would remove the user section :
http://localhost/devit/users/test/12/123
Then use uri->segment(4, 'default_value'); for the user_id.

The uri class documentation may be of use to you.

Hope it helps.
#3

[eluser]mddd[/eluser]
You can use $uri->uri_to_assoc(). It makes the uri parts into an associative array.
For instance if you call /controller/method/id/123/name/john, then $uri->uri_to_assoc() will give you array ('id'=>123, 'name'=>'john').

You can combine this with 'fixed' variables (not having a name, like your value '12'. You can do this by giving the number of the first segment where you want to start the 'name/value' parts. Normally that number is 3 because the first is the controller, second is the method and then the parameters start. But you could have /controller/method/12/id/123/name/john and get $uri->uri_to_assoc(4) and that would get you the same array as before. You can get the first parameter seperately using $uri->segment(3).
#4

[eluser]Lyon[/eluser]
[quote author="mddd" date="1279892292"]You can use $uri->uri_to_assoc(). It makes the uri parts into an associative array.
For instance if you call /controller/method/id/123/name/john, then $uri->uri_to_assoc() will give you array ('id'=>123, 'name'=>'john').

You can combine this with 'fixed' variables (not having a name, like your value '12'. You can do this by giving the number of the first segment where you want to start the 'name/value' parts. Normally that number is 3 because the first is the controller, second is the method and then the parameters start. But you could have /controller/method/12/id/123/name/john and get $uri->uri_to_assoc(4) and that would get you the same array as before. You can get the first parameter seperately using $uri->segment(3).[/quote]

That's pretty cool.
Actually helps me with my site as well.
Thanks!
#5

[eluser]mddd[/eluser]
You're welcome. I still find useful functions in CI.. it's worth it to take the time to read the entire manual, even if you have before! There's lots of useful stuff in there.
#6

[eluser]vindhyareddy[/eluser]
I have been using this only to get the parameters when they are given a parameter name also in the beginning.

But this does not help when parameter name is not given!
#7

[eluser]mddd[/eluser]
Like I said, if you have parameters without a name in the beginning, you can start the 'name/value' parameters wherever you like by giving the number of the first segment. Of course you have to get the parameters before that yourself using $this->uri->segment(x);




Theme © iAndrew 2016 - Forum software by © MyBB