我正在创建一个显示行的页面。每行有两列是我的问题、评分和使用的重点。首次加载页面时,没有预排序,页面显示正常。工作时的代码:
if (!$sortby) {
$getdata = mysql_query("select * FROM traditional") or die(mysql_error());
while($data = mysql_fetch_array($getdata)){
$category = $data["category"];
$overview = $data["overview"];
$ratingcount = $data["ratingcount"];
$ratingscore = $data["ratingscore"];
$usage = $data["usage"];
$location = $data["location"];
$calculaterating = $ratingscore / $ratingcount;
if (!$calculaterating) {
$rating = "None";
} else {
$rating = $calculaterating;
}
echo "</tr>";
echo "<tr colspan='4'>";
echo "<td class='selectcategory' width='40%'>";
echo "<a href='$location'>$category</a>";
echo "</td>";
echo "<td class='categorymenu' style='vertical-align: middle; text-align: left;
padding: 5px;' width='40%'>";
echo $overview;
echo "</td>";
echo "<td class='categorymenu' width='10%'>";
echo $rating;
echo "</td>";
echo "<td class='categorymenu' width='10%'>";
echo $usage;
echo "</td>";
}
这就是事情停止工作的地方。页面上有一个下拉菜单,允许用户按使用情况或评级进行排序。当他们选择一个时,页面会刷新,并且 $sortby 会根据评分或使用情况填充。这是代码的其余部分,当事情停止工作时:
} else {
if ($sortby = "rating") {
$sort = "rating";
} else {
$sort = "`usage`";
}
$getdata = mysql_query("select category,overview,SUM(ratingscore / ratingcount) as
'rating',`usage`,location FROM traditional ORDER BY $sort DESC") or die(mysql_error());
while($data = mysql_fetch_array($getdata)){
$category = $data["category"];
$overview = $data["overview"];
$rating = $data["rating"];
$usage = $data["usage"];
$location = $data["location"];
echo "</tr>";
echo "<tr colspan='4'>";
echo "<td class='selectcategory' width='40%'>";
echo "<a href='$location'>$category</a>";
echo "</td>";
echo "<td class='categorymenu' style='vertical-align: middle; text-align: left;
padding: 5px;' width='40%'>";
echo $overview;
echo "</td>";
echo "<td class='categorymenu' width='10%'>";
echo $rating;
echo "</td>";
echo "<td class='categorymenu' width='10%'>";
echo $usage;
echo "</td>";
}
}
未填充 $sortby 时,将显示大约 10 行。由于代码不起作用(当尝试对结果进行排序时),仅显示一行(不知道为什么?),并且显示的一行评级无论如何都不会正确计算。我已经阅读了许多其他与此相关的帖子(这有助于我编写目前拥有的糟糕代码)。但我想我只是不明白我从别人的帖子中得到了什么。