如何在PHP中对数据库创建的图表中的项目进行排序?

问题描述:

I have created a database which contains people from a run with the amount of rounds which they ran. After that I created this script which creates a html chart.

The script:

<?php
  $db = new PDO("mysql:host=HIDDEN;dbname=HIDDEN", 'HIDDEN', 'HIDDEN') or die ("Verbindung nicht möglich");
  $query_temp = $db->prepare("SELECT * FROM runners");
  $query_temp->bindParam(':id', $id);
  $id = 1;
  $query_temp->execute();

  echo '<table class="table table-hover">';
  echo '<tr>';
  echo '<th>ID</th>';
  echo '<th>Name</th>';
  echo '<th>Runden</th>';
  echo '</tr>';
  while($id_table = $query_temp->fetch(PDO::FETCH_ASSOC)) {
    echo '<tr>';
    echo '<td>'.$id_table['runner_id'].'</td>';
    echo '<td>'.$id_table['runner_name'].'</td>';
    echo '<td>'.$id_table['runner_rounds'].'</td>';
    echo '</tr>';
  }
  echo '</table>';
?>

The Database: http://img.prntscr.com/img?url=http://i.imgur.com/WklMcTG.png

Screenshot how it looks: http://img.prntscr.com/img?url=http://i.imgur.com/v2zbOSu.png

Now I want to give the users the ability to sort the people for their amount of rounds which they ran. Like the most person is at the first place. How can I do this?

To make this you need a js plugin and turn the HTML dynamic. There is a lot of plugins to make it and will list some of them to you:

  1. datatables
  2. tablesorter

Each plugin work differently, but in the end they have almost the same behaviour, then you need to check the documentation of them and try to run...

You should use DataTables with jQuery