tags:

views:

99

answers:

1

I want to write a function to make user click the table header double click and change into descending or ascending order, and I have finished the sorting function, but don't know how to put together, now I just put one of them into the header as following:

function sortBy(sKey)
{
   document.sortResultsForm.sSortBy.value=sKey;
   document.sortResultsForm.submit();
}

if($sSortBy=="District")
{
   usort($tempArr, 'sortDistrictNameascend');
}

 <table border="0" width="100%" cellspacing="2" cellpadding="2">
   <tr> <td class="headTable" width=15%>
     <a href="javascript:sortBy('District');"
       ><font color=white>District</font></a>
   </td>

and I don't know how to put the other function into the header let user can double click to change sorting order, the order function as following:

function sortDistrictNamedescend($a, $b)
{
    if ($a->DistrictName == $b->DistrictName)
    {
        return 0;
    }
    return ($a->DistrictName < $b->DistrictName) ? 1 : -1;
}

usort($tempArr, 'sortDistrictNamedescend');

anybody can help me, thank you very much.

+2  A: 

You need to check a 'sort-order' variable:

mypage.php?sort=asc and mypage.php?sort=desc

Within your PHP script, you'll check the value of $_GET["sort"]; if its' "desc" you give print the data in reverse. If it's "asc" you print it alphabetically.

<?php

  $data = get_data();

  if ($_GET["sort"] == "desc")
    reverse_data();

  # print data

?>

Because javascript cannot communicate directly with PHP, your only option is to pass the values through the URL or through a Form.

Jonathan Sampson