所以現在我的phpmyadmin上有我的MySql數據庫連接到我的PHP腳本。它列出了去年50個不同的NFL球員和他們的統計數據。我希望能夠列出一個下拉框,以便我可以根據任何類別對球員進行排序(即接待,記錄YDS,TD等),但我不知道該怎麼做。我在那裏有一個switch語句,但它現在似乎沒有做任何事情。用MySQL和PHP排序
<!DOCTYPE html>
<html>
<!-- Seth Rataiczak -->
<head>
<title>PHP Project</title>
<style>
table,th,td {
border:1px solid navy;
}
body {
background-color:peachpuff;
}
</style>
</head>
<body>
<?php
// database connection
$db_hostname='localhost';
$db_username='root';
$db_password='';
$db_database='Project';
$connection = new mysqli( $db_hostname,
$db_username,
$db_password,
$db_database);
//MySQL Select Statement
$sort = "";
if(isset($_GET['sort'])) {
switch ($_GET['sort']) {
case 0:
$sort = ' ORDER BY Team DESC';
break;
case 1:
$sort = ' ORDER BY Pos DESC';
break;
case 2:
$sort = ' ORDER BY Rec DESC';
break;
case 3:
$sort = ' ORDER BY Yds DESC';
break;
case 4:
$sort = ' ORDER BY Avg DESC';
break;
case 5:
$sort = ' ORDER BY Yds/G DESC';
break;
case 6:
$sort = ' ORDER BY TD DESC';
break;
}
}
$sql = "SELECT * FROM NFL_2014_Receiving WHERE Field=1" . $sort;
$result = $connection->query($sql);
if (!$result) die ($connection->error);
$n = $result->num_rows;
$nfl = array();
// echos the table headers
echo "<table>
<tr><th>ID</th><th>Player</th><th>Team</th>
<th>Position</th><th>Receptions</th>
<th>Receiving Yards</th><th>Avg Yds/Catch</th>
<th>Avg Yds/Game</th><th>Touchdowns</th></tr>";
// echos the table data
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
$nfl[$row['iD']] = $row['Player'];
if(!isset($_POST['hide']) || $_POST['hide'] != $row['iD']){
echo "<tr><td width=20>" . $row['iD'] . "</td><td width=150>" . $row['Player'] . "</td><td width=40>" .
$row['Team'] . "</td><td width=30>" . $row['Pos'] . "</td><td width=30>" .
$row['Rec'] . "</td><td width=40>" . $row['Yds'] . "</td><td width=30>" .
$row['Avg'] . "</td><td width=40>" . $row['Yds/G'] . "</td><td width=20>" .
$row['TD'] . "</td></tr>";
}
}
echo "</table><br>";
//dropdown box
echo "<form method='post' action='index.php'><select name='hide'>";
foreach($nfl as $key=>$value){
echo "<option value='".$key."'>".$value."</option>";
}
// submit button
echo "<input type='submit' value='Submit'>";
echo "</select></form>";
?>
</body>
</html>
您確定'$ sort'正在設置嗎? –
@JayBlanchard不,我不確定。我在代碼 – Rataiczak24