我创建了一个测验页面,用于将登录的用户测验结果存储到排行榜。
我有一个名为members的表,其中包含user和quiz_score列。我根据登录用户的测验结果打印出排行榜。我在其个人资料页面上的排行榜中显示特定用户位置时遇到问题。如果他们在前三名中,我还想附上奖章(例如jpeg图片)。 这是我到目前为止排行榜的代码:
<?php
require_once 'header.php';
// Send variables for the MySQL database class.
$database = mysql_connect('localhost', 'root', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('robinsnest') or die('Could not select database');
$query = "SELECT * FROM `members` ORDER by `quiz_score` DESC LIMIT 10";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_results = mysql_num_rows($result);
echo '<div class="container marketing">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
<table class="gradienttable">
<tr>
<th>Position</th>
<th>User Name</th>
<th>Score</th>
</tr>';
for($i = 1; $i <= $num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<tr>
<td>".$i."</td>
<td>".$row['user']."</td>
<td>".$row['quiz_score']."</td>
</tr>";
}
echo "</table>
</div>
</div>";
echo"<hr class='featurette-divider'>";
echo ' <footer>
<p class="pull-right"><a href="#">Back to top</a></p>
<p>2015 Students-NCI, · <a href="#">Privacy</a> · <a href="#">Terms</a></p>
</footer>';
?>
</body>
</html>
到目前为止我用于打印用户分数的代码:
<?php
require_once 'header.php';
$database = mysql_connect('localhost', 'root', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('robinsnest') or die('Could not select database');
$query = "SELECT user, quiz_score, FIND_IN_SET( quiz_score, (
SELECT GROUP_CONCAT( quiz_score
ORDER BY quiz_score DESC )
FROM members )
) AS rank
FROM members
WHERE user = '$user';";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_results = mysql_num_rows($result);
echo "<p>".$query."</p>";
echo"<hr class='featurette-divider'>";
echo ' <footer>
<p class="pull-right"><a href="#">Back to top</a></p>
<p>2015 Students-NCI, · <a href="#">Privacy</a> · <a href="#">Terms</a></p>
</footer>';
?>
</body>
</html>
不打印出登录用户的位置只是sql查询的字符串。我尝试了一些不同的东西但没有成功。
非常感谢任何帮助。
答案 0 :(得分:2)
for loop()
,而是使用while loop()
进行查询。我知道你通过查询结果的数量来循环它,但它只会得到第一行的值。如果您分配循环的当前编号并将其作为数组附加到变量,则可能是可行的。$ranking
的计数器,它会在每个循环上递增(删除$num_results
,因为你不再需要它了。)?>
附上PHP。while loop
来显示数据(如果查询正确)。您的固定代码:
<?php
require_once 'header.php';
// Send variables for the MySQL database class.
$database = mysql_connect('localhost', 'root', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('robinsnest') or die('Could not select database');
$query = "SELECT * FROM `members` ORDER by `quiz_score` DESC LIMIT 10";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_results = mysql_num_rows($result);
$ranking = 1;
?>
<div class="container marketing">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
<table class="gradienttable">
<tr>
<th>Position</th>
<th>User Name</th>
<th>Score</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $ranking; ?></td>
<td><?php echo $row['user']; ?></td>
<td><?php echo $row['quiz_score']; ?></td>
<?php
$ranking = $ranking + 1; /* INCREMENT RANKING BY 1 */
?>
</tr>
<?php
} /* END OF WHILE LOOP */
?>
</table>
</div>
</div>
<hr class="featurette-divider">
<footer>
<p class="pull-right"><a href="#">Back to top</a></p>
<p>2015 Students-NCI, · <a href="#">Privacy</a> · <a href="#">Terms</a></p>
</footer>
</body>
</html>
您的第二个代码:
<?php
require_once 'header.php';
$database = mysql_connect('localhost', 'root', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('robinsnest') or die('Could not select database');
$query = "SELECT user, quiz_score, FIND_IN_SET( quiz_score, (
SELECT GROUP_CONCAT( quiz_score
ORDER BY quiz_score DESC )
FROM members )
) AS rank
FROM members
WHERE user = '$user';";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_results = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
?>
<p><?php echo $row['user']." - ".$row['quiz_score']; ?></p>
<?php
} /* END OF WHILE LOOP */
?>
<hr class="featurette-divider">
<footer>
<p class="pull-right"><a href="#">Back to top</a></p>
<p>2015 Students-NCI, · <a href="#">Privacy</a> · <a href="#">Terms</a></p>
</footer>';
</body>
</html>
prepared statement
代替已弃用的mysql_*
函数来阻止SQL injections。