Posts: 6,236
Threads: 310
Joined: Jan 2011
Reputation:
0
I've only been using mySQL for a few months on my server, so I only really know the basics. Could anyone point me in the right direction on how to display a top ten leaderboard (for example 'Score' in the table 'players') in a textdraw?
Thanks in advance.
Posts: 3,793
Threads: 196
Joined: Jan 2010
Reputation:
0
TKAdmin has an example, check it out. It includes the page.
Posts: 6,236
Threads: 310
Joined: Jan 2011
Reputation:
0
Looks to be that's a website panel. I need this in samp.
Posts: 6,129
Threads: 36
Joined: Jan 2009
pawn Code:
mysql_query("SELECT `Name`, `Score` FROM `players` ORDER BY `Score` DESC LIMIT 10");
mysql_store_result();
new szPrepTD[460], iIterator = 1, iScore, szName[MAX_PLAYER_NAME], szUnload[64];
while(mysql_fetch_row_format(szUnload)) {
// This code will repeat ten times.
sscanf(szUnload, "p<|>sd", szName, iScore);
if(strlen(szPrepTD) == 0) format(szPrepTD, sizeof(szPrepTD), "#%d - %s (%d score)\n", iIterator, szName, iScore);
format(szPrepTD, sizeof(szPrepTD), "%s#%d - %s (%d score)\n", szPrepTD, iIterator, szName, iScore);
iIterator++;
}
mysql_free_result();
Use 'szPrepTD' as the string to set your textdraw. Something like this will work, I haven't tested this though. Rename fields as necessary and whatnot.
Posts: 6,236
Threads: 310
Joined: Jan 2011
Reputation:
0
So will that send 10 queries?
Posts: 6,129
Threads: 36
Joined: Jan 2009
No, there is only one query. But 10 records are stored in memory, and the while() code retrieves the results from memory in to the code within the while code block.
Posts: 6,236
Threads: 310
Joined: Jan 2011
Reputation:
0
Okay, thanks. Could you just explain what sccanf does in this code? I don't use it.
Posts: 6,129
Threads: 36
Joined: Jan 2009
I was too lazy to use a split function in this example, but because mysql_fetch_row_format has multiple results to return, it returns them in this format:
The sscanf splits that string in to a string and an integer so you can use iScore and szName. It splits it by the '|' (vertical pipe) character.
I just edited the code now because I missed something in the sscanf code.
Posts: 6,236
Threads: 310
Joined: Jan 2011
Reputation:
0
When I use it, I get spam in my logs:
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
[14:14:39] sscanf warning: Strings without a length are deprecated, please add a destination size.
How do I fix this?
Posts: 6,129
Threads: 36
Joined: Jan 2009
pawn Code:
sscanf(szUnload, "p<|>s[24]d", szName, iScore);
Wasn't really thinking when I wrote the code originally, sorry. Try the code above.