我有一个表,该表由mysql数据库中的数据填充,我需要能够单击表的某些列以对它们进行升序和降序排序,如果我可以使用php,我不确定如何进行此操作html或javascript,我将附加一张图片以显示我已经拥有的图像,以便更好地了解我在说什么 https://imgur.com/a/ypnNxB0
<?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>";
?>
答案 0 :(得分:2)
有各种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");