CodeIgniter Forums
Segment-based URL vs Query Strings - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: General Help (
+--- Thread: Segment-based URL vs Query Strings (/showthread.php?tid=62977)

Segment-based URL vs Query Strings - wishmaster - 09-13-2015

I write application and need some help.

E.g. the page "Customers" has a lot of rows and I use the filter form on the top of page with fields: "Name","Filial","Status" and so on. I need "track" filtered result when clicking edit item and return to the search page. Yes, I can achieve this with query string like below

But what about segment-based. Is it possible?


RE: Segment-based URL vs Query Strings - mwhitney - 09-14-2015

In most cases, the segment-based alternative to the URL you've supplied would be something like or (you could also filter out the index.php with mod_rewrite, if you wished).

Generally, the first format is easier to support, as you would just setup your Customers controller's index() method to accept two arguments (client_name and status), like this:

PHP Code:
class Customers extends MY_Controller
   // ... 
function index($client_name ''$status '')
       if ($client_name !== '') {
           // do something with $client_name
       if ($status !== '') {
           // do something with $status

The biggest drawback to this method is that they have to be supplied in a consistent order. If you use the second form (where the name of each section precedes the value, much like the original query string), you can change the order, but you have to spend a little more time mapping the arguments to variables in your controller. The URI class provides a method for getting the URI segments in an associative array from the name/value format called uri_to_assoc().

RE: Segment-based URL vs Query Strings - wishmaster - 09-14-2015

Thanks for reply. I know about uri_to_assoc.

My problem was in allowed characters in config-file and I am unable to set in the name field value like "Hello World!" as encodeURIComponent does not encode !, ', (, ), and *.

My code

$('#button-filter').on('click', function() {

        var url = '<?php echo base_url('client'); ?>';

        var filter_name = $('input[name=\'filter_name\']').val();

        if (filter_name) {
                url += '/' + fixedEncodeURIComponent(filter_name);

        var filter_filial = $('input[name=\'filter_filail\']').val();

        if (filter_filial) {
                url += '/' + encodeURIComponent(filter_filial);

        var filter_rate = $('input[name=\'filter_rate\']').val();

        if (filter_rate) {
                url += '/' + encodeURIComponent(filter_rate);

        var filter_status = $('select[name=\'filter_status\']').val();

        if (filter_status != "*") {
                url += '/' + encodeURIComponent(filter_status);

        location = url;
So I thought about switching to query string approach.
But suddenly I have found the solution. Just small fix to encodeURIComponent.

P.S. I am not a coder but I LOVE CI! It's fantastic! :-)

RE: Segment-based URL vs Query Strings - mwhitney - 09-14-2015

Yes, as long as you pass data through the URL, whether in a query string or as a URI segment, it will be limited to valid URL characters. Otherwise, you would have to pass the data as part of a post (in a form or using something like $.post() in jQuery), which would also mean that users would not be able to use a specific URL to return to the filtered results.