A switch between ascending and descending table rows on a tableheader click

后端 未结 1 480
我寻月下人不归
我寻月下人不归 2021-01-07 11:30

I\'ve been messing with this for quite some time now, but I just can\'t get it to work. What I\'m trying to make is ;

A clickable table header, that once clicked swi

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-07 12:07

    Just add a link to your table header cell which contains a parameter, e.g.

    
       
          Name
       
    
    

    What happens here? A link will be added, containing an order parameter which is set to the opposite of the current order value(1/true or 0/false) or to 1 as default.

    In your PHP script you can now decide how to order your table using the order value:

    $isAsc = isset($_GET['order'])? (bool) $_GET['order']: 1;
    

    Now you can use the $isAsc boolean:

    if ($isAsc) {
       // Sort data ascending
    } else {
       // Sort data descending
    }
    

    Or in a query:

    $sql = "SELECT * FROM tabe ORDER BY name ".($isAsc?"ASC":"DESC").";";
    

    Of course, you can extend this idea, e.g., by adding column names to sort by several columns.

    0 讨论(0)
提交回复
热议问题