问题描述
我创建了一个测验页面,用于将登录用户的测验结果存储到排行榜中.
I have created a quiz page that stores a logged in users quiz result to a leaderboard.
我有一个名为members的表,其中包含user和quiz_score列.根据登录用户进行测验的结果,我可以打印出排行榜.我在个人资料页面的排行榜上显示特定用户的位置时遇到问题.如果它们在前三名中,我还想附加一枚奖章(例如jpeg图片).这是到目前为止我用于排行榜的代码:
I have a table called members with the columns user and quiz_score. I have a leaderboard printing out depending on the result of a quiz taken by the logged in user. I am having problems displaying the specific users position in the leaderboard on their profile page. I would also like to attach a medal (e.g jpeg image) if they are in the top three.here is the code I have so far for the leaderboard:
<?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>
到目前为止,我拥有用于打印出用户分数的代码:
And the code I have for printing out the user score so far:
<?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查询的字符串就不会打印出登录用户的位置.我尝试了一些不同的方法,但没有成功.
Is not printing out the position of the logged in user just the string of the sql query. I have tried out a few different things but have had no success.
任何帮助将不胜感激.
推荐答案
注意:
- 我删除了您的
for loop()
,而是使用while loop()
进行查询.我知道您通过查询结果的数量来循环它,但是它只会得到第一行的值.如果您分配当前的循环号并将其作为数组附加到变量,也许是可行的. - 我使用了一个名为
$ranking
的计数器,该计数器将在每个循环中递增(已删除$num_results
,因为您不再需要它了.) - 您正在以错误的方式混合使用PHP和HTML.在继续使用HTML之前,先用
?>
将PHP括起来. - 清理了一些代码.
- 第二个给定代码的目的是什么?如果您已经在尝试在第一个代码中显示数据?但是我仍然尝试通过获取数据并使用
while loop
来显示数据(如果查询正确). - 确保查询正确(连接凭据,数据库名称,表名称,列名称等)
- I removed your
for loop()
and instead use awhile loop()
for your query. I know you loop it by the number of results of the query, but it will only get the value of the first row. Maybe it is feasible if you assign the current number of the loop and attach it to the variable as array. - I used a counter named
$ranking
that will increment on every loop (removed the$num_results
, for you don't need it anymore). - You are mixing PHP and HTML the wrong way. Enclosed PHP by
?>
before proceeding with an HTML. - Cleaned up some of your codes.
- What is the purpose of your second given code? If you are already trying to display the data in your first code? But I still try to display the data (if the query is correct) by fetching the data and using
while loop
. - Make sure that your queries are correct (credentials of your connection, database name, table name, column name, etc.)
Note:
您的固定代码:
<?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_*
函数,以防止. - You should be using
prepared statement
instead of deprecatedmysql_*
functions to prevent SQL injections.
Recommendation:
这篇关于显示用户在排行榜表php mysql中的排名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!