views:

23

answers:

1

i see that in tableSorter you can sort one page at a time which concerns me.

Only 1 page of results can be sorted at a time?, which is pretty limiting. If you have a query result that spans multiple pages, how you will handle this?

If anyone knows better, feel free to correct me if iam incorrect

Thanks.

A: 

I would assign an ID to the th in thead for the table which correspond to you columns in the SQL-table.

$("table thead th").click(function(){
    $.getJSON('ajax/get_results.php', 
        {sortby: $(this).attr('id')},
        function(data) {
            $.each(data, function(){
                // fill table here
            }
        });   
});

You could create some function for sorting the table that you also could use for changing page with AJAX if you are not already doing it.

And on the backend then sort with SQL

"SELECT * FROM table ORDER BY ".$_GET['sortby']

Of course you should secure that sortby, for example filter it with an array of allowed values. I would suggest creating an array with allowed column sort names and using the array_intersect() function to only filter out the allowed values or just check with

if(isset($allowed_columns[$_GET['sortby']]))

Then just output it all in JSON by putting all the results in an array and then:

echo json_encode($array_with_results);

I guess you would like to output somthing like this in JSON:

{
  {col1: 'row1',col2: 'row1',col3: 'row1'},
  {col1: 'row2',col2: 'row2',col3: 'row2'},
  {col1: 'row3',col2: 'row3',col3: 'row3'}
}
alexteg
thanks, but i dont know php, have you by any chance done in asp.net?
Abu Hamzah
Well, I don't know asp.net on the other hand. But the JavaScript would be the same and also MsSQL has ORDER BY. So you just need some function for creating the JSON. If ASP does not have it you could do it manually with a loop. But I think you can find some method here http://json.org/ or otherwise you can see the JSON structure on Wikipedia: http://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example
alexteg