1

i have a php page that displays a table from multiple MySQL queries and from their use a JavaScript function to sort the column results, all that works fine, my problem is i need to refresh the results of those queries every 10 seconds or so which works fine(with a meta-refresh), the problem is the refresh after column sorting. when the page refreshes the sorting is reset also. here is a snippet of the sorting function;

    <script>
    function tablesort(which){  <-----I tried using the $_GET method you suggested
                                <-----But i get a "missing formal parameter" error
                                <-----When also using this suggestion and use the
                                <-----"onclick" i get a "tablesort" is not defined
                                <-----error
$(document).ready(function(){
if(which == '1.0'){<!--This sorts the pause row, descending --> 
$("#Mtable").tablesorter({sortList: [[1,0]]});
}
if(which == '2.1'){<!--This sorts the total dialer row, descending --> 
$("#Mtable").tablesorter({sortList: [[2,1]]});
}
if(which == '3.0'){<!--This sorts Wrap-up time row, descending --> 
$("#Mtable").tablesorter({sortList: [[3,0]]});
}
if(which == '4.1'){<!--This sorts donation amount row, descending --> 
$("#Mtable").tablesorter({sortList: [[4,1]]});
}
if(which == '5.1'){<!--This sorts Up-sale row, descending --> 
$("#Mtable").tablesorter({sortList: [[5,1]]});
}
if(which == '6.1'){<!--This sorts the Monthl donation row, descending --> 
$("#Mtable").tablesorter({sortList: [[6,1]]});
}
if(which == '7.1'){<!--This sorts the verified sales row, descending --> 
$("#Mtable").tablesorter({sortList: [[7,1]]});
}
if(which == '8.1'){<!--This sorts the calles per hour row, descending --> 
$("#Mtable").tablesorter({sortList: [[8,1]]});
}
if(which == '9.1'){<!--This sorts the payments per hour row, descending --> 
$("#Mtable").tablesorter({sortList: [[9,1]]});
}
if(which == '10.1'){<!--This sorts the average sale row, descending --> 
$("#Mtable").tablesorter({sortList: [[10,1]]});
}
if(which == '11.1'){<!--This sorts the sales total row, descending --> 
$("#Mtable").tablesorter({sortList: [[11,1]]});
}
    });
    }
    </script>

here are the links that sort the table'

        Sort by: 
<a onclick="tablesort('1.0')"> Lowest Pause<a/>&nbsp &nbsp
<a onclick="tablesort('2.1')"> Highest Dialer<a/>&nbsp &nbsp
<a onclick="tablesort('3.0')"> Best Wrap-up<a/>&nbsp &nbsp

because of the refresh im wanting to pass the variable data from the onlcick to the URL similar to $_GET so it would would something like and then read into the sorting function;

localhost/dbtabke.php?which=2.1 <----exact URL example being used

any help on how to do that would be greatly appreciated, thanks in advance.

@prabeen giri i have provided the complete function, thanks again

Iz3k34l
  • 130
  • 2
  • 10
  • `$.get('localhost/dbtabke.php?Mtable=' + sort_by_var, ...)`? – Marc B Apr 10 '13 at 15:38
  • You want to pass the query variables as part of an AJAX request that reloads the data via the PHP page. – Matthew Darnell Apr 10 '13 at 15:36
  • @Mathew Darnell, I am wanting to pass the ""tablesort('1.0')" data to the javascript function through the URL, because of the refresh part of it – Iz3k34l Apr 10 '13 at 15:39
  • You should have access to that JS function via your PHP page when you get the new data so you should just have to pass tablesort='1.0' via the GET request, then sort the data before it's rendered. – Matthew Darnell Apr 10 '13 at 15:43

2 Answers2

0

You don't necessarily have to use GET method to retain the sort order.

You can also use the cookie to store the sort order.In that way your code will look more cleaner.

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
} 

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

Call the setCookie() function when you sort the order and pass the sort order as parameter.

And when the page loads for the first time or refreshes, call the same sort function and call getCookie() to get the cookie value and set that as your sort order before sorting.

If you want to use GET, this will also do the work when page refreshes , I hope you call this function tablesort() when document is ready.

tablesort('<?php print $_GET['MTable']?>') ;

Note: check the parameter is valid or not in the tablsort() function because when the page loads for the first time , GET variable will be empty , I believe

prabeen giri
  • 803
  • 7
  • 18
0

I ended doing the following using $_GET,here is the code;

    <body>
    <!--This gets the element from the URL to set the Sorting, so the page
        can be refreshed without losing the sorting-->
    <body onload="sorttable.innerSortFunction.apply(document.getElementById('<?php echo $_GET["id"]; ?>'), [])">

    <!--table headers that are used to determine columns to sort by-->
    echo "<th id=\"dialertime\">".ucfirst("Dial Time")."</th>\n";
    echo "<th id=\"pausetime\">".ucfirst("Pause Time")."</th>\n";

    ...*other table info*
    <!--links that sort the specific row-->
    <a href="?id=wrap-by-time"> Wrap-up time<a/>&nbsp &nbsp
    <a href="?id=wrap-by-percent"> Wrap-up %<a/>&nbsp &nbsp
    </body>

thanks for everyone's input, hope it helps

Iz3k34l
  • 130
  • 2
  • 10