SQLite TOP players -
Ner0x96 - 20.10.2016
Ey all! I'm trying to make a players TOP, but it doesn't show the rank, I was trying with differents ways but it still with showing the rank..
I'm using this code:
Код:
new string[128], query[256], ranks;
new DBResult:result;
new DBResult:result2;
format(query,sizeof(query),"SELECT `NAME` FROM `USERS` ORDER BY (`RANK` * 1) DESC limit 10");
result = db_query(Database, query);
for(new a;a<db_num_rows(result);a++)
{
db_get_field(result,0,string,128);
format(query,sizeof(query),"SELECT `RANK` FROM `USERS` WHERE `NAME` = lower('%s')", string);
result2 = db_query(Database,query);
ranks = db_get_field_int(result2, 5);
new str[40];
format(str, sizeof(str), "%d. %s - Rank: %d", a+1, string, ranks);
printf(str);
//SendFMessage(playerid, white, "%d. %s - Rank: %d", a+1, string, ranks);
db_next_row(result);
db_free_result(result2);
}
db_free_result(result);
samp-server.exe shows this:
Regards.
PS. Sorry for my bad English, I'm a Spanish guy :v
Re: SQLite TOP players -
Konstantinos - 20.10.2016
All can be done in 1 query and you don't need formatting (the query) too. When selecting 1 field, the fieldid would be 0 and not 5.
PHP код:
new rank_info[200], player_name[MAX_PLAYER_NAME], DBResult: result = db_query(Database, "SELECT `NAME`,`RANK` FROM `USERS` ORDER BY `RANK` DESC limit 10");
for (new a; db_next_row(result) != 0; a++)
{
db_get_field(result, 0, player_name, sizeof player_name);
format(rank_info, sizeof(rank_info), "%d. %s - Rank: %d", a + 1, player_name, db_get_field_int(result, 1));
// client message
}
db_free_result(result);
// or dialog..
Re: SQLite TOP players -
Ner0x96 - 20.10.2016
Quote:
Originally Posted by Konstantinos
All can be done in 1 query and you don't need formatting (the query) too. When selecting 1 field, the fieldid would be 0 and not 5.
PHP код:
new rank_info[200], player_name[MAX_PLAYER_NAME], DBResult: result = db_query(Database, "SELECT `NAME`,`RANK` FROM `USERS` ORDER BY `RANK` DESC limit 10");
for (new a; db_next_row(result) != 0; a++)
{
db_get_field(result, 0, player_name, sizeof player_name);
format(rank_info, sizeof(rank_info), "%d. %s - Rank: %d", a + 1, player_name, db_get_field_int(result, 1));
// client message
}
db_free_result(result);
// or dialog..
|
Oh thanks, now it's working but doesn't show the higher one from the DB:
I should be the first on the list but I don't appear there :V
Re: SQLite TOP players -
SyS - 20.10.2016
Thats because you order it by descending order. Remove DESC
Re: SQLite TOP players -
Konstantinos - 20.10.2016
I see, Gammix's trick seems to skip the first row so change the loop to:
PHP код:
for (new a, rows = db_num_rows(result); a < rows; a++)
{
db_get_field(result, 0, player_name, sizeof player_name);
format(rank_info, sizeof(rank_info), "%d. %s - Rank: %d", a + 1, player_name, db_get_field_int(result, 1));
// client message
db_next_row(result);
}
Re: SQLite TOP players -
Ner0x96 - 20.10.2016
Quote:
Originally Posted by Sreyas
Thats because you order it by descending order.
|
(??) I'm rank 35 on the DB, (I've created these ranks and these things with some users on my PC) so I should be the number 1 (1. _Ner0x[JMPR] - Rank: 35)
Quote:
Originally Posted by Konstantinos
I see, Gammix's trick seems to skip the first row so change the loop to:
PHP код:
for (new a, rows = db_num_rows(result); a < rows; a++)
{
db_get_field(result, 0, player_name, sizeof player_name);
format(rank_info, sizeof(rank_info), "%d. %s - Rank: %d", a + 1, player_name, db_get_field_int(result, 1));
// client message
db_next_row(result);
}
|
Now it's not working lol:
Re: SQLite TOP players -
Konstantinos - 20.10.2016
Err, are you sure you placed db_next_row inside the loop? It should have worked, at least I don't see any mistake myself.
Re: SQLite TOP players -
SyS - 20.10.2016
Quote:
Originally Posted by Ner0x96
(??) I'm rank 35 on the DB, (I've created these ranks and these thinks with some users on my PC) so I should be the number 1 (1. _Ner0x[JMPR] - Rank: 35)
Now it's not working lol:
|
i misinterpreted your question i thought ranks should be get by ascending order as thats normal case no? -_-
try this
PHP код:
rows = db_num_rows(result);
for (new a; a < rows; a++,db_next_row(result))
{
db_get_field(result, 0, player_name, sizeof player_name);
format(rank_info, sizeof(rank_info), "%d. %s - Rank: %d", a + 1, player_name, db_get_field_int(result, 1));
// client message
}
Re: SQLite TOP players -
Ner0x96 - 20.10.2016
Quote:
Originally Posted by Konstantinos
Err, are you sure you placed db_next_row inside the loop? It should have worked, at least I don't see any mistake myself.
|
Lol I forgot it, my wrong sorry.
It's working now, thanks all for helping me <3
Re: SQLite TOP players -
Ner0x96 - 21.10.2016
Nvm, fixed, I forgot to use strcat XD