简体   繁体   中英

Trying to display top 10 scores - Only getting one result

I'm trying to create a leaderboards page which shows the top 10 users depending on how much money they have. The code that I have created works, but only shows one result -- the user with the most money. The problem is probably something simple, but I can't find anything to help.

Here is my code:

<?php

include "globals.php";

print "
<div class='generalinfo_txt'>
<div><img src='images/info_left.jpg' alt='' /></div>
<div class='info_mid'><h2 style='padding-top:10px;'> Leaderboards </h2></div>
<div><img src='images/info_right.jpg' alt='' /></div> </div>
<div class='generalinfo_simple'><br> <br><br>";

$q=$db->query("SELECT * FROM users ORDER BY money DESC LIMIT 10;");
print "<table width=75% cellspacing=1 class='table'> <tr style='background:gray;'> <th>Username</th> <th>User Level</th><th>Amount</th> </tr>";
while($r=$db->fetch_row($q))
{
$money=money_formatter($r['money']);
print "<tr><td>{$r['username']}";
print "</td><td>{$r['level']}</td><td>{$money}</td></tr>";
print "</table></div><div><img src='images/generalinfo_btm.jpg' alt='' /></div><br></div></div></div></div></div>";
}
$h->endpage();
?>

I can see a flaw in the code here. Your line:

print "</table></div><div><img src='images/generalinfo_btm.jpg' alt='' /></div><br></div></div></div></div></div>";

should be after the while loop, not inside it. As it is, you get broken HTML and you finish the table after the first result is printed.

您正在使用仅提取一条记录的fetch_row,是否意味着要使用fetch_array提取所有10条记录?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM