i有一个由MySQL数据库中数据填充的表,我需要能够单击表的某些列以对它们进行排序,以对它们进行排序和下降,我不确定如何如果我可以使用PHP HTML或JavaScript,请使用此图像,我将附加图像以显示我已经必须更好地了解我在说什么https://i.stack.imgur.com/qhqgi.jpg
<?php
$connection = mysqli_connect('localhost', 'root', '','nba201819'); //The Blank string is the password
$result = mysqli_query($connection,"SELECT * FROM `teamstats` ORDER BY `teamstats`.`WIN%` DESC");
?>
<table id="teamstats" border ='2'>
<tr>
<th></th>
<th>Code</th>
<th>Team</th>
<th>GP</th>
<th>W</th>
<th>L</th>
<th><a href='?sortBy=WIN%'>WIN%</th>
<th>MIN</th>
<th><a href='?sortBy=PTS'>PTS</th>
<th><a href='?sortBy=FGM'>FGM</th>
<th>FGA</th>
<th>FG%</th>
<th><a href='?sortBy=3PM'>3PM</th>
<th>3P%</th>
<th><a href='?sortBy=FTM'>FTM</th>
<th>FTA</th>
<th>FT%</th>
<th>OREB</th>
<th>DREB</th>
<th><a href='?sortBy=REB'>REB</th>
<th>AST</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><img src='logos/".$row['TEAMCODE']."_logo.svg' width =20 height=20></td>" ;
echo "<td>" . $row['TEAMCODE'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['GP'] . "</td>";
echo "<td>" . $row['W'] . "</td>";
echo "<td>" . $row['L'] . "</td>";
echo "<td>" . $row['WIN%'] . "</td>";
echo "<td>" . $row['MIN'] . "</td>";
echo "<td>" . $row['PTS'] . "</td>";
echo "<td>" . $row['FGM'] . "</td>";
echo "<td>" . $row['FGA'] . "</td>";
echo "<td>" . $row['FG%'] . "</td>";
echo "<td>" . $row['3PM'] . "</td>";
echo "<td>" . $row['3P%'] . "</td>";
echo "<td>" . $row['FTM'] . "</td>";
echo "<td>" . $row['FTA'] . "</td>";
echo "<td>" . $row['FT%'] . "</td>";
echo "<td>" . $row['OREB'] . "</td>";
echo "<td>" . $row['DREB'] . "</td>";
echo "<td>" . $row['REB'] . "</td>";
echo "<td>" . $row['AST'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
有各种JS插件可以帮助实现数据表排序,例如EasyUI,DataTables等。
如果您只想像提供的简单代码实现它,则必须检索sortBy
变量并将其放入SQL查询中:
$connection = mysqli_connect('localhost', 'root', '','nba201819'); //The Blank string is the password
if (isset($_GET['sortBy'])) {
if ($_GET['sortBy'] !== '') {
$sortBy = str_replace( "`", "``", $_GET['sortBy']);
} else {
$sortBy = 'WIN%';
}
} else {
$sortBy = 'WIN%';
}
$result = mysqli_query($connection,"SELECT * FROM `teamstats` ORDER BY `teamstats`.`$sortBy` DESC");