View Single Post
Old 03/12/2019, 01:11 PM   #6
Kwarde's Avatar
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,712
Reputation: 1671
Default Re: Fetch offline and online players

How to get all files: You could use FileManager to fetch all dirs/files from a directory:

However I also recommend you to use MySQL (or SQLite). Looping through /reading (evenually writing) all files could create serious laggs (remember that SA-MP is single-threaded; If you would loop/read all files, nobody would be synced untill this action is finished! They can't chat or use commands etc...).

With MySQL you could do it like this. Le's assume we have three tables:
Table `fations`:
[ID] [Name]     [Owner]
1    MyFaction  Kwarde
1    MyFaction2 Kwarde

Table `faction_members`:
[ID] [Faction_ID] [Member]
1     1              1
2     1              5
3     1              3            
4     2              1

Table `users`:
[ID] [Name]
1    Kwarde
2    Merel
3    Petah
4    x_fjskd29
5    John_Doe
To get all user names from faction 1 and the name of faction ID 1 you could use:

SELECT a1.`Name` AS 'Faction_Name', a2.`Name` AS 'User_Name' FROM `factions` a1 JOIN `users` a2 WHERE a1.`ID` = 1 AND a2.`ID` IN (SELECT `Member` FROM `faction_members` WHERE `Faction_ID` = 1)
This query would return:
[Faction_Name] [User_Name]
MyFaction      Kwarde
MyFaction      Petah
MyFaction      John_Doe
Assuming you're using a threaded query you won't have laggs because the MySQL server will do the work (which is way faster than reading file-by-file). You should really consider using MySQL or SQLite for this.

I've nothing against using files but for functions like this you definitely don't want to use files.

To work with above query ingame (to fetch results):
//In the callback that's called after using _tquery()
new num_rows;
if (!num_rows)
    return; //There are no results, end function
for (new i; i < num_rows; i++)
    new Faction_Name[30], User_Name[25];
    cache_get_value_name(i, "Faction_Name", Faction_Name);
    cache_get_value_name(i, "User_Name", User_Name);

    //You can now use variables 'Faction_Name' and 'User_Name' to show data to a player
When the opportunity presents itself to flip-a da table, uh, you flip-a da table.

Discord: Kwarde#8009

Last edited by Kwarde; 25/12/2019 at 11:08 AM.
Kwarde is offline   Reply With Quote