views:

775

answers:

2

I'm implementing a search box using CodeIgniter, but I'm not sure about how I should pass the search parameters through.

I have three parameters - the search string, product category, and the sort order. They're all optional.

Currently, I'm sending the parameters through POST to a temporary method which forwards the parameters to the regular URI form. This works fine.

But I'm using a weird URI format: e.g.

http://site.com/products/search=computer,sort=price,cat=laptop

Does anyone have a better/cleaner format of passing stuff through? I was thinking of passing it into the products method as arguments, but since the parameters are optional things would get messy.

Or should I suck it up and just turn GET methods on? :O

A: 

If you're using a fixed number of parameters, you can assign a default value to them and send it instead of not sending the parameter at all. For instance

 http://site.com/products/search/all/somevalue/all

Next, in the controller you can ignore the parameter if (parameter == 'all'.)

 Class Products extends Controller {
 ...

     // From your code I assume that this your structure.
     function index ($search = 'all', $sort = 'price', $cat = 'all')
     {
         if ('all' == $search)
         {
            // don't use this parameter
         }
         // or
         if ('all' != $cat)
         {
            // use this parameter
         }
         ...
     }
     ...
 }
Ch4m3l3on
+2  A: 

Query Strings

You can enable query strings in CodeIgniter to allow a more standard search function.

Config.php

$config['enable_query_strings'] = FALSE;

Once enabled, you can accept the following in your app:

http://site.com/products/search?term=computer&sort=price&cat=laptop

The benefit here is that the user will find it easy to edit the URL to make a quick change to their search, and your search uses common search functionality.

The down side of this approach is that you are going against one of the design decisions of the CodeIgniter development team. However, my personal opinion is that this is OK provided that query strings are not used for the bulk of your content, only for special cases such as search queries.

Jon Winstanley
The only problem I see with this is that it doesn't work well with the built-in pagination class. Any advice?
v3