3

I was looking for a way to pass "GET" variables in codeigniter and ended up coming across this : link text

I am wondering how to implement it.

For example :

www.website.com/query would give me every entry in the DB .

Typically I would have

www.website.com/query/?id=5 to get the equivalent entry.

when i try to do that the CI way :

www.website.com/query/id/5

I get a 404 error since it is looking for a class named id and it can't find it.

is there any way to get a step by step way to do this?

thank you.

Community
  • 1
  • 1
salmane
  • 4,799
  • 13
  • 48
  • 61

3 Answers3

6

Two good ways to achieve this using methods intended by the Codeigniter developers.

OPTION ONE:

If you always expect an "id" parameter to be present you could take advantage of a feature where you pass the value in the URI immediately after the method (function) you want to call.

Example passing /[controller]/[method]/[value]:

http://www.website.com/query/index/5

You would then access the value of "id" as an expected parameter of the function.

Class Query extends Controller {
...

    // From your URL I assume you have an index method in the Query controller.
    function index($id = NULL)
    {
        // Show current ID value.
        echo "ID is $id";
        ...
    }
    ...
}

OPTION TWO:

If you would like to allow many parameters to be passed in addition to ID, you could add all parameters as key=>value pairs to the URI segments in any order.

Example passing /[controller]/[method]/[key1]/[val1]/[key2]/[val2]/[key3]/[val3]:

http://www.website.com/query/index/id/5/sort/date/highlight/term

You would then parse all the URI segments from the 3rd segment ("id") forward into an array of key=>value pairs with the uri_to_assoc($segment) function from the URI Class.

Class Query extends Controller {
...

    // From your code I assume you are calling an index method in the Query controller.
    function index()
    {
        // Get parameters from URI.
        // URI Class is initialized by the system automatically.
        $data->params = $this->uri->uri_to_assoc(3);
        ...
    }
    ...
}

This would give you easy access to all the parameters and they could be in any order in the URI, just like a traditional query string.

$data->params would now contain an array of your URI segments:

Array
(
    [id] => 5
    [sort] => date
    [highlight] => term
)

HYBRID OF ONE AND TWO:

You could also do a hybrid of these where ID is passed as an expected parameter and the other options are passed as key=>value pairs. This is a good option when ID is required and the other parameters are all optional.

Example passing /[controller]/[method]/[id]/[key1]/[val1]/[key2]/[val2]:

http://www.website.com/query/index/5/sort/date/highlight/term

You would then parse all the URI segments from the 4th segment ("sort") forward into an array of key=>value pairs with the uri_to_assoc($segment) function from the URI Class.

Class Query extends Controller {
...

    // From your code I assume you are calling an index method in the Query controller.
    function index($id = NULL)
    {
        // Show current ID value.
        echo "ID is $id";

        // Get parameters from URI.
        // URI Class is initialized by the system automatically.
        $data->params = $this->uri->uri_to_assoc(4);
        ...
    }
    ...
}

$id would contain your ID value and $data->params would contain an array of your URI segments:

NexusRex
  • 2,152
  • 17
  • 14
1

You can still use GET parameters, they're just mapped to controller member function parameters:

test.com/query/id/4

Would map to the controller:

$query->id($id);

This assumes you have added a query controller and member function properly in the controllers folder in your CI application.

You can also pass your parameter values as POST parameters using a form and the CI input class.

Bruce Alderson
  • 1,517
  • 13
  • 19
0

Use $this->uri->uri_to_assoc(2) 2 is an offset, as you starting your associative array of segments in the 2nd segment. You will also need a route to make /query map to a controller and method (unless you do this in the index() method).

So, this URL:

/query/id/foo/key/bar

could be read using:

$get = $this->uri->uri_to_assoc(2);

echo $get['id']; // 'foo'
echo $get['key']; // 'bar'

It's not great, but it works.

Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117
  • 1
    You mentioned that it isnt great, what would be a better alternative? – salmane Nov 26 '10 at 13:05
  • If there was a better alternative I would have suggested it. It would be better if CodeIgniter didnt munge the $_GET variable in the first place, but that's something we'll hopefully get sorted for 2.0 or the Community Branch. – Phil Sturgeon Nov 28 '10 at 22:04
  • This actually IS a great way to use URI segments as variables, and is an intended method by the CI developers! – NexusRex Dec 01 '10 at 17:55
  • The URL has a mistake though... it should point to a controller and a method... /query/index/id/foo/key/bar – NexusRex Dec 01 '10 at 17:56
  • The other alternative, as I outlined above, uses expected (required) values. ".../query/index/foo/bar/var3" and your function would define each as expected parameters: "function index($id = NULL, $key = NULL, $var3 = NULL)" – NexusRex Dec 01 '10 at 18:00
  • It's "not great" as query strings should work out of the box. It's the best way to do it, but you shouldn't have to do it at all. – Phil Sturgeon Dec 01 '10 at 23:03
  • Follow-up, query strings will work out the box in CodeIgniter Reactor. I love commit access :D – Phil Sturgeon Dec 15 '10 at 15:38