Search code examples
phpregextablesorter

How to parse URL when using the pager plugin with AJAX


I am trying to use the tablesorter pager plugin with AJAX but run into som problemes (or limitations) when trying to handle the AJAX request in my php backend.

If eg. the table is set up with a default sorting of

sortList: [ [0,1], [1,0] ]

I will get a URL like this on my AJAX request:

page=0&size=50&filter=fcol[6]=batteri&sort=col[0]=1&col[1]=0

In my php back end I do a

$cur_sort = $_GET['sort']

and get

col[0]=1

So the last part is missing - I guess since it contains a & char.

How do I get the entire sort string?

That said how is the string col[0]=1&col[1]=0 best parsed? I need to extract the info that col 0 is to be sorter DESC and col 1 ASC.


Solution

  • You can try this;

    parse_str($_SERVER['QUERY_STRING'],$data);
    

    It will parse the url to an array;

    Also; you should use empty [] instead of [1] and [0]

    See more here: parse_str()

    Example:

    $str = "page=0&size=50&filter=fcol[6]=batteri&sort=col[0]=1&col[1]=0";
    
    parse_str($str, $output);
    echo $output['page'];  // echo 0
    

    And to answer your question; it is correct; is echoing col[0]=1 because you are dividing with & see here:

    &sort=col[0]=1 & col[1]=0;

    An advice; use more names, instead. You could use

    &sort[]=1&sort[]=0;

    UPDATE: To access the last one; you should do, simply;

    $_GET['col'][1];
    

    If you want to access, the last number in

    $_GET['sort'];
    

    You can do this;

    $explode = explode('=',$_GET['sort']);
    $end = end($explode);
    echo $end; //it will outout 1
    

    If you print your entire query_String, it will print this;

    Array
    (
        [page] => 0
        [size] => 50
        [filter] => fcol[6]=batteri
        [sort] => col[0]=1
        [col] => Array
            (
                [1] => 0
            )
    
    )