
enum Data
{
ID,
Name
};
new GroupInfo[Amount_of_groups][Data];
public OnGameModeInit()
{
//Start the group loading. It's in another function to keep it clean.
LoadGroups();
}
forward LoadGroups();
public LoadGroups()
{
//Execute a SQL query to get all records from the groups table
//We tell the function to pass data onto OnGroupsRetrieved.
mysql_tquery(sqlHandle, "SELECT * FROM `groups_table`;", "OnGroupsRetrieved");
}
//Called on completion of LoadGroups query
forward OnGroupsRetrieved();
public OnGroupsRetrieved()
{
//First check if there are any entries. If not, print that to console.
if(cache_get_row_count() == 0) print("[INFO]: No groups found in database.");
else
{
//Now that we know we have data, execute a for loop for the amount of entries in the database.
//Same as above, we use cache_get_row_count to get the amount of result rows we have from the query
for(new i = 0; i < cache_get_row_count(); i++)
{
//cache_get_field_content_int wants a result number and the name of the field to get the value from.
//We use 'i' as result number because we loop through the results
GroupInfo[i][ID] = cache_get_field_content_int(i, "ID");
//cache_get_field_content is a little different. It too wants the result number and field name, but I always
//compare it with the 'format' function. This because it also wants the variable to put the value into in the function
//and it wants the length of the variable. In addition to that, it also wants the sqlHandle variable.
//cache_get_field_content(resultnumber, fieldname, variable to store into, sqlhandle, length of variable to store into);
cache_get_field_content(i, "Name", GroupInfo[i][Name], sqlHandle, GROUP_NAME_LENGTH);
}
//Once we loaded everything we print the amount of rows of our result, just for information.
printf("[INFO]: Loaded %d groups from the database.", cache_get_row_count());
}
return true; //I believe you should return true to 'free result'; empty the cache.
}
|
You could either take the approach you described above, or query the database once a user tries to create a group. I wrote a small example with comments for the approach you described above.
It's just a quick and basic example, so copy/pasting may not work (nor was it intended to be a copy pasting example). Hope this gives you a basic picture on a way to do what you want. If anything's still unclear, I'll be happy to help and explain. PHP код:
|
new groups;
public OnGameModeInit()
{
mysql_tquery(mysql, "SELECT * FROM `groups`", "GetLines");
return 1;
}
forward GetLines();
public GetLines()
{
groups=cache_get_row_count();
}
enum Data
{
ID,
Name
};
new GroupInfo[groups][Data];
error 008: must be a constant expression; assumed zero
groups=cache_get_row_count();
if(groups == 0) print("[INFO]: No groups found in database.");
else
{
print("----------------------------------------------------------------");
print(" ");
print("\tLOADING GROUPS");
print(" ");
for(new i = 0; i < groups; i++)
{
GroupInfo[i][ID] = cache_get_field_content_int(i, "ID");
cache_get_field_content(i, "Name", GroupInfo[i][Name], mysql, 12);
printf("NAME : %s",GroupInfo[i][Name]);
}
printf("[LOADING GROUPS] : Loaded %d groups from the database.", groups);
print(" ");
print("----------------------------------------------------------------");
}
for(new i=0; i<groups; i++) printf("NAME : %s",GroupInfo[i][Name]);
|
new problem
Код:
groups=cache_get_row_count();
if(groups == 0) print("[INFO]: No groups found in database.");
else
{
print("----------------------------------------------------------------");
print(" ");
print("\tLOADING GROUPS");
print(" ");
for(new i = 0; i < groups; i++)
{
GroupInfo[i][ID] = cache_get_field_content_int(i, "ID");
cache_get_field_content(i, "Name", GroupInfo[i][Name], mysql, 12);
printf("NAME : %s",GroupInfo[i][Name]);
}
printf("[LOADING GROUPS] : Loaded %d groups from the database.", groups);
print(" ");
print("----------------------------------------------------------------");
}
for(new i=0; i<groups; i++) printf("NAME : %s",GroupInfo[i][Name]);
[18:46:31] ---------------------------------------------------------------- [18:46:31] [18:46:31] LOADING GROUPS [18:46:31] [18:46:31] NAME : dadac [18:46:31] NAME : cvcvvvsvsa [18:46:31] NAME : dqdaaacxzc [18:46:31] NAME : xccsa [18:46:31] NAME : yyyya [18:46:31] [LOADING GROUPS] : Loaded 5 groups from the database. [18:46:31] [18:46:31] ---------------------------------------------------------------- [18:46:31] NAME : dcd xyyyya [18:46:31] NAME : cd xyyyya [18:46:31] NAME : d xyyyya [18:46:31] NAME : xyyyya [18:46:31] NAME : yyyya I dont do anything to GroupInfo[i][Name] after storing group name into it, but it gets all messed up outside the for cycle. |
enum Data
{
ID,
Name[32]
};
new GroupInfo[Amount_of_groups][Data];
cache_get_field_content(i, "Name", GroupInfo[i][Name], mysql, 32);
|
Thank you for you detailed answer, it was very helpful. Perhaps you also know how to get the amount of rows for enum? There can be different amount of groups everytime OnGameModeInit gets called so I tried doing this
-- code -- it isn't much of a problem as I can just give that array a big value like new GroupInfo[1000][Data], I'm just curious if it's possible to do it ![]() |
#define MAX_GROUPS 20
new GroupInfo[MAX_GROUPS][Data];
|
Name is a string variable, which contains characters, you have to set it's lenght.
Код:
enum Data
{
ID,
Name[32]
};
new GroupInfo[Amount_of_groups][Data];
Код:
cache_get_field_content(i, "Name", GroupInfo[i][Name], mysql, 32); |