/top10 with mysql.
#1

Hi all.Maybe anybody can show me how to do top10 of players with mysql?and with my postion for ex:

You are a position 1..
Reply
#2

Lets just say that we are doing the top 10 players by score. Your variables are probably not going to be like the following but it's a example

pawn Code:
mysql_query("SELECT score, username FROM usertable ORDER BY score DESC LIMIT 10");
mysql_store_result();
new id;
while(mysql_retrieve_row())
{
    id ++;
    new username[24], scorevariable[5];
    mysql_fetch_field_row(username, "username");
    mysql_fetch_field_row(scorevariable, "score");
    printf("Position %d: Username - %s | Score: %d", id, username, strval(scorevariable));
}
mysql_free_result();
Reply
#3

But i think it's get only my position.

How i can do with dialog like that:

pawn Code:
1.Player1 - %d XP
2.Player2 - %d XP
3.Player3 - %d XP
4.Player4 - %d XP
5.Player5 - %d XP
6.Player6 - %d XP
7.Player7 - %d XP
8.Player8 - %d XP
9.Player9 - %d XP
10.Player10 - %d XP

Your position in top is %d.With score %d
Reply
#4

pawn Code:
// Declare this before starting the loop
new
    output[512], // Make this larger if you need to
    tmpStr[64];

// This should be in the loop, after you fetched the row from the db
format(tmpstr, sizeof(tmpstr), "%d. %s - %d XP\n", id, username, score);
strcat(output, tmpStr);

// And then after mysql_free_result
ShowPlayerDialog(playerid, 1234, DIALOG_STYLE_LIST, "Table Name", output, "Ok", "");
Reply
#5

Thank you both.But i want to ask one more thing.How to make "your postion is %d of %d? i have mean i have do with my postion,but i don't know how to do of what position your are if max registered players is 10 then will be

Your position is 2 of 10
Reply
#6

You could either use the count function in SQL or do the following

pawn Code:
stock ReturnUserCount()
{
    mysql_query("SELECT username FROM  `usertable`");
    mysql_store_result();
    new users = mysql_num_rows();
    mysql_free_result();
    return users;
}
I wouldn't really recommend calling that often if you had a big database.
Reply
#7

HiC >>> why not just mysql_row_count?
Reply
#8

I try to do but i don't know you to do and with your position? i try to do:

pawn Code:
mysql_query("SELECT Exp, Name FROM playerdata ORDER BY Exp DESC LIMIT 10");
mysql_store_result();
new id,output[512],username[24], scorevariable[5],tmpStr[64],tmpStr2[64];
while(mysql_retrieve_row())
{
id ++;
mysql_fetch_field_row(username, "Name");
mysql_fetch_field_row(scorevariable, "Exp");
format(tmpStr, sizeof(tmpStr), "%d. %s %d XP\n", id, username, strval(scorevariable));
strcat(output, tmpStr);
}
format(tmpStr2, sizeof(tmpStr2), "Your position is %d of %d with %d XP",id,ReturnUserCount(),strval(scorevariable));
strcat(output, tmpStr2);
mysql_free_result();
ShowPlayerDialog(playerid, 8, DIALOG_STYLE_MSGBOX, "players top10", output, "OK", "");
return 1;
}
But,i don't get players for ex:

pawn Code:
Player1 = 2000XP
Player2 = 1000XP
I'am "Player1" and when i write /top10 i only get my

pawn Code:
1.Player1 2000XP
but i want to do that:

pawn Code:
1.Player1 2000XP
2.Player2 1000XP
pawn Code:
mysql_query("SELECT Exp, Name FROM playerdata ORDER BY Exp DESC LIMIT 10");
Then how "DESC LIMIT 10" and limit will be not 10,but that how much is registered players.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)