English 中文(简体)
Not using $_GET parameters
原标题:

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_products/all_categories/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".

最佳回答

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

问题回答

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
  }
}

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.

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]





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签