Welcome Guest, Not a member yet? Register   Sign In
Segment-based URL vs Query Strings
#1

Hi,
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

http://example.com/index.php?c=customers...tus=active

But what about segment-based. Is it possible?

Thanks!
Reply
#2

In most cases, the segment-based alternative to the URL you've supplied would be something like http://example.com/index.php/customers/i...ill/active or http://example.com/index.php/customers/i...tus/active (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
{
 
   // ... 
 
   
    public 
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().
Reply
#3

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

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.

https://developer.mozilla.org/en-US/docs...IComponent

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

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB