gpt4 book ai didi

php - 使用 MySQL 和 PHP 进行排序

转载 作者:搜寻专家 更新时间:2023-10-30 22:10:46 24 4
gpt4 key购买 nike

现在我将 phpmyadmin 上的 MySql 数据库连接到我的 PHP 脚本。它列出了 50 名不同的 NFL 球员和他们去年的统计数据。我希望能够列出一个下拉框,我可以在其中按任何类别(即接待、Rec Yds、TD 等)对球员进行排序,但我不确定我将如何做到这一点……??我在那里有一个 switch 语句,但它现在似乎没有做任何事情。

<!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>

最佳答案

您正在设置 POST 方法操作,并且正在搜索 GET 以获取值。

明确一点:您可以更改表单方法以获取 <form method='get' action='index.php'>或更改您的 php 值 $_GET['sort']$_POST['sort']

我很确定这是你的问题,但你可以回显你的 $sql 变量来查看你的查询做了什么;)

关于php - 使用 MySQL 和 PHP 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30037874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com