0

I have a parent class which is loading the data from Dataset. Child class is accessing the values and printing them in a table. There are 10 rows with name, surname and more. I would like to sort name values when someone click on name <th> using plain php without any javascript or mysql. Here is my code

$table = "<table border='1'>" .
         "<tr>" . "<th>" . "ID" . "</th>";
         "<th>" . "<button id='name'>" .  "Name" . "</button>" .  "</th>" .
         "<th>" . "Surname" . "</th>" .
         "<th>" . "Active" . "</th>" .
         "<th>" . "Last Login" . "</th>" .
         "<th>" . "Picture" . "</th>" .
         "<th>" . "<button id='rating'>" .  "Rating" . "</button>" ."</th>" . "</tr>";
        echo $table;
        //showing the data in rows
        foreach ($user_data as $data) {
            echo "<tr>" . "<td>" . $data->id . "</td>";
            echo "<td>" . $data->name . "</td>";
            echo "<td>" . $data->surname . "</td>";
            echo "<td>" . $data->active . "</td>";
            echo "<td>" . $data->last_login . "</td>";

Note: I don't want to introduce new loop. I want to stay in one loop.

Qasim Ali
  • 587
  • 2
  • 11
  • 28

1 Answers1

0

You can use sorting. But what you are asking for is already been covered in Datatables API. You can use the JS framework Datatables API and then make a cleaner back-end program to handle the API with a post request. If you want to do it from scratch I suggest you go for Jquery event handling with sorting or do a API trigger with sorting.

Sohan Arafat
  • 93
  • 2
  • 16