views:

218

answers:

4

In working with CodeIgniter, it appears $_GET is disabled by default. I'm wondering why this is.

A lot of times, I want to build very long search queries. So for example, I have a form that allows you to search the database by N different fields. In code igniter, the url to display my search result would be:

http://mysite.com/field1/field2/field3/.../fieldN-1/fieldN

So an example url would be

http://mysite.com/shopping/toys/educational/age6-8/page1/sortbypriceinascendingorder/

I don't particularly like this because:

1) what if i want to add more search parameters at a later time such that we have something like:

http://mysite.com/shopping/toys/education/age6-8/page1/sortbypriceinascendingorder/boys-only/in-stock

I don't like how I'm adding "boys-only" and "in-stock" at the end of the page/sortby segments of the url. It doesn't feel right.

2) what if a person doesn't use the "toy" segment and "educational" segment? Then the url looks kind of clumsy

http://mysite.com/shopping/all%5Fproducts/all%5Fcategories/age6-8/page1/sortbypriceinascendingorder/

Doesn't it make more sense to use $_GET parameters for search because then the order in which you place query string parameters (&field=value) doesn't matter? And omitting a query string parameter automatically means "not selected".

+2  A: 

First, you can always enable $_GET variables if you want to.

Also, you could use params like ../shopping/type:toys/cat:education/age:6-8/sort:price_asc, and then parse them in controller code:

function shopping() {
  $args = func_get_args();
  foreach ($args AS $arg) {
    list($filter_name, $filter_value) = explode(':', $arg, 2);
    if ($filter_name == 'cat') {
      // set category filter to $filter_value (education)
    } elseif ($filter_name == 'type') {
      // set type filter to toys
    }
    // etc etc
  }
}
Joel L
+1  A: 

The reason WHY they do this is that it interferes with the calling of controllers, functions, and parameters.

CI uses the URI string to tell the "program" what to do. There is a mode you can enable that makes this:

www.example.com/controller/method/parameter

into this:

www.example.com/?c=controller&m=methods&p=parameter1&yourgetitem=yourgetvalue

Not as pretty though, but you sure CAN do whatever you like with CI; it just takes some tweaking.

Alex Mcp
+1  A: 

You could also use a different delimiter for search parameters, like + (as many systems do). These are passed to a single controller route, where you can explode() as required. Example: http://http://mysite.com/shopping/toys+education+age8+etc

Bruce Alderson
+1  A: 

It is possible to use both query strings and segments in CodeIgniter, here's how:

In config.php set:

  • $config['uri_protocol'] = "PATH_INFO";
  • $config['enable_query_strings'] = TRUE;

In .htaccess use a / after index.php instead of a ? on your CI rule: RewriteRule ^(.*)$ index.php/$1 [L]

bradym