如何将两个功能放在一起

时间:2021-08-11 21:17:20

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.

任何人都可以帮助我,非常感谢你。

1 个解决方案

#1


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

您需要检查'sort-order'变量:

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

mypage.php?sort = asc和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脚本中,您将检查$ _GET [“sort”]的值;如果它的'desc'你反向打印数据。如果它是“asc”,则按字母顺序打印。

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

由于javascript无法直接与PHP通信,因此您唯一的选择是通过URL或通过表单传递值。

#1


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

您需要检查'sort-order'变量:

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

mypage.php?sort = asc和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脚本中,您将检查$ _GET [“sort”]的值;如果它的'desc'你反向打印数据。如果它是“asc”,则按字母顺序打印。

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

由于javascript无法直接与PHP通信,因此您唯一的选择是通过URL或通过表单传递值。