我不确定为什么我的代码无法排序,我对此很陌生,任何帮助将不胜感激。
这是读取内容的文件,然后应该对其进行排序,但是当我在浏览器上执行它时,对数据进行排序的链接没有任何影响。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Movie Reviews Display</title>
</head>
<body>
<?php
$host="XXXXXXX";
$username="XXXXXX"; // Mysql username
$password="XXXXXXX"; // Mysql password
$db_name="XXXXXXXX"; // Database name
$tbl_name="movies"; // Table name
$con=mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db($con,"XXXXXX") or die ("no database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM movies");
?>
<h1>Movie Reviews Database</h1>
<hr />
<p />
<?php
echo "<table border='1'>
<tr>
<th>Review Date</th>
<th>Title</th>
<th>Rating</th>
<th>Genre</th>
<th>Release Date</th>
<th>Reviewer Name</th>
<th>Review Rateing</th>
<th>Review Text</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['reviewDate'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['rating'] . "</td>";
echo "<td>" . $row['genre'] . "</td>";
echo "<td>" . $row['releaseDate'] . "</td>";
echo "<td>" . $row['ReviewerName'] . "</td>";
echo "<td>" . $row['reviewRating'] . "</td>";
echo "<td>" . $row['reviewText'] . "</td>";
echo "</tr>";
}
echo "</table>";
if(isset($_GET['sort'])){
switch ($_GET['sort'] ){
case 0:
$query = "SELECT * FROM movies ORDER BY reviewDate DESC";
mysqli_query($con,$query);
break;
case 1:
$query = 'SELECT * FROM movies ORDER BY title DESC';
mysqli_query($con,$query);
break;
case 2:
$query = 'SELECT * FROM movies ORDER BY rating DESC';
mysqli_query($con,$query);
break;
case 3:
$query = 'SELECT * FROM movies ORDER BY genre DESC';
mysqli_query($con,$query);
break;
case 4:
$query = 'SELECT * FROM movies ORDER BY releaseDate DESC';
mysqli_query($con,$query);
break;
case 5:
$query = 'SELECT * FROM movies ORDER BY ReviewerName DESC';
mysqli_query($con,$query);
break;
case 6:
$query = 'SELECT * FROM movies ORDER BY reviewRating DESC';
mysqli_query($con,$query);
break;
}
}
mysqli_close($con);
?>
<p />
Sort by...
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=0";?>">Review Date</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=1";?>">Title</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=2";?>">Rating</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=3";?>">Genre</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=4";?>">Release Date</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=5";?>">Reviewer</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=6";?>">Reviewer Rating</a>
</body>
</html>
最佳答案
我会让它变得更加复杂,但我保留了你的总体想法并进行了改进。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Movie Reviews Display</title>
</head>
<body>
<?php
$host = "XXXXXXX";
$username = "XXXXXX";
$password = "XXXXXXX";
$db_name = "XXXXXXXX";
$tbl_name = "movies";
$con = mysqli_connect($host, $username, $password);
if(mysqli_connect_errno())
$con = false;
else
$cdb = mysqli_select_db($con, $db_name);
if(!$con) {
echo "Failed to connect to database server!";
else if(!$cdb) {
echo "Failed to select database!";
else {
echo " <h1>Movie Reviews Database</h1>
<hr />
<p />
<table border='1'>
<tr>
<th>Review Date</th>
<th>Title</th>
<th>Rating</th>
<th>Genre</th>
<th>Release Date</th>
<th>Reviewer Name</th>
<th>Review Rateing</th>
<th>Review Text</th>
</tr>\r\n";
$sort = "";
if(isset($_GET['sort'])) {
switch ($_GET['sort'] ) {
case 0:
$sort = " ORDER BY reviewDate DESC";
break;
case 1:
$sort = ' ORDER BY title DESC';
break;
case 2:
$sort = ' ORDER BY rating DESC';
break;
case 3:
$sort = ' ORDER BY genre DESC';
break;
case 4:
$sort = ' ORDER BY releaseDate DESC';
break;
case 5:
$sort = ' ORDER BY ReviewerName DESC';
break;
case 6:
$sort = ' ORDER BY reviewRating DESC';
break;
}
}
$result = mysqli_query($con, "SELECT * FROM `movies`" . $sort);
while($row = mysqli_fetch_array($result)) {
echo " <tr>\r\n";
echo " <td>" . $row['reviewDate'] . "</td>\r\n";
echo " <td>" . $row['title'] . "</td>\r\n";
echo " <td>" . $row['rating'] . "</td>\r\n";
echo " <td>" . $row['genre'] . "</td>\r\n";
echo " <td>" . $row['releaseDate'] . "</td>\r\n";
echo " <td>" . $row['ReviewerName'] . "</td>\r\n";
echo " <td>" . $row['reviewRating'] . "</td>\r\n";
echo " <td>" . $row['reviewText'] . "</td>\r\n";
echo " </tr>\r\n";
}
echo " </table>\r\n";
}
mysqli_close($con);
?>
<p />Sort by...
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=0";?>">Review Date</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=1";?>">Title</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=2";?>">Rating</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=3";?>">Genre</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=4";?>">Release Date</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=5";?>">Reviewer</a>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?sort=6";?>">Reviewer Rating</a>
</body>
</html>
关于php - 尝试在 php 中对 mysql 表中的数据表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20359836/