02.12.2014, 11:16
(I tried to mark all MySQL Db things as bold and all serverside table / serverside DB things as italic)
You're having 2 Databases / Tabels - On the one hand you've got your MySQL Database, which is storing each weapon in a new line, with the first line starting with ID 1 - this is the
first line number (so line 1 -> ID 1)
To save bandwhich and such, you transfere the MySQL Database to the server itself. You create a "table" (so an array in your case). You can access each line with a parameter in your brackets (index)
However the table on your server does NOT start with line 1 -> ID 1! Here the first line has the index 0, so line 1 -> INDEX 0
Basicly each line on your server should have the index of SQLID-1 -> one number lower
When getting data from the MySQL Table you need to "translate" / convert them, to access the right line in the server's table, as the ID != INDEX
GetWepEOIndexFromSQL will tell you the INDEX of the line you are searching for from your server's table, when you know the MySQL ID.
so
means
Now you start defining some variables. We call the variable that will store our result "rst". But as the first INDEX in your table is 0, we define it as rst = -1! We do this incase we won't find
any matching line. As we use -1 we won't get the first line in the case there actually is no line.
The following part
askes the MySQL DB for the amount of all lines in the table, as we may need this in the loop below.
We coult although also use "sizeof(WepSQLID)" instead of "maxr" in the "for" loop
Now the part that you might actually struggle with:
Our loop. We start with n = 0 and increase it each time until we reach maxr -> the amount of lines we have in our MySQL Database. We use n < maxr as we start with n = 0! If we would go to
n = maxr we would go increase it by one number to much (n = 0 - first line [ID1]; n = 1 - second line [ID2]; ...; n = maxr-1 - last line [IDMAX])
Now we compare
weapid is the parameter we gave the function at the top (stock GetWepIndexFromSQL(weapid)). As told above, weapid is the ID of the MySQL Table (so in the ideal case it should be n + 1).
WepSQL is a table that connects the MySQL Table and the table of your server! It is ordered the same way as your weapon table in your server is:
The first field in the table would be the INDEX of the weapon (the one that is assigned on your serverside table). The second field is the ID of the weapon (the one that is used in the MySQL DB).
it's like:
We know the ID and want to know the index.
Therefor we use
n is the "index". As WepSQLID[INDEX] returns the ID assigned to the specific INDEX we just check whether the ID we know (weapid) is the same as we have in the actual table line.
E.g. if we had ID 4 (GetWepIndexFromSQL(4))
We would start with n = 0.
now
is called
As we have a matching line now, we can remove the "-1" we assigned to the "rst" before and replace it with the actual line, that the line in the MySQL DB would have in the serverside table.
We just found out that the index we were searching for is "3" - the one that is assigned to our "n". So we assign this to the rst variable.
At this point we know which line/index in the serverside table we need and we have stored in a variable. Therefor there is no need in going on with our loop anymore, as we suppose there is only
1 matching line. As we have found it, we can stop searching the through the following lines -> we stop the loop with
We
-> meaning the function now tells you
You're having 2 Databases / Tabels - On the one hand you've got your MySQL Database, which is storing each weapon in a new line, with the first line starting with ID 1 - this is the
first line number (so line 1 -> ID 1)
To save bandwhich and such, you transfere the MySQL Database to the server itself. You create a "table" (so an array in your case). You can access each line with a parameter in your brackets (index)
However the table on your server does NOT start with line 1 -> ID 1! Here the first line has the index 0, so line 1 -> INDEX 0
Basicly each line on your server should have the index of SQLID-1 -> one number lower
When getting data from the MySQL Table you need to "translate" / convert them, to access the right line in the server's table, as the ID != INDEX
GetWepEOIndexFromSQL will tell you the INDEX of the line you are searching for from your server's table, when you know the MySQL ID.
so
pawn Код:
stock GetWepIndexFromSQL(weapid)
Код:
Tell me the table INDEX, the ID I know is 'weapid' (weapid->MySQL ID!)
any matching line. As we use -1 we won't get the first line in the case there actually is no line.
The following part
pawn Код:
new string[40];
format(string, sizeof(string), "SELECT `id` FROM `weapons`");
mysql_query(string);
mysql_store_result();
new maxr = mysql_num_rows();
mysql_free_result();
We coult although also use "sizeof(WepSQLID)" instead of "maxr" in the "for" loop
Now the part that you might actually struggle with:
pawn Код:
for(new n=0; n<maxr; n++)
n = maxr we would go increase it by one number to much (n = 0 - first line [ID1]; n = 1 - second line [ID2]; ...; n = maxr-1 - last line [IDMAX])
Now we compare
pawn Код:
if(weapid == WepSQLID[n])
WepSQL is a table that connects the MySQL Table and the table of your server! It is ordered the same way as your weapon table in your server is:
The first field in the table would be the INDEX of the weapon (the one that is assigned on your serverside table). The second field is the ID of the weapon (the one that is used in the MySQL DB).
it's like:
Код:
index 0 => id 1 index 1 => id 2 index 2 => id 3 index 3 => id 4 etc...
Therefor we use
pawn Код:
weapid == WepSQLID[n]
E.g. if we had ID 4 (GetWepIndexFromSQL(4))
We would start with n = 0.
Код:
We had "weapid == WepSQLID[n]" ==> "4 == WepSQLID[0]" - that would be false, as as we can see in the list above, index 0 as ID 1 assigned, which is obviously not ID 4 So we skip the rest and start again, but this time with n = 1 "weapid == WepSQLID[n]" ==> "4 == WepSQLID[1]" - still false, as index 1 has ID 2 assigned. So again "4 == WepSQLID[2]" - false, as index 2 => ID 3 "4 == WepSQLID[3]" - TRUE! index 3 has ID 4 assigned!
pawn Код:
rst = n;
break;
As we have a matching line now, we can remove the "-1" we assigned to the "rst" before and replace it with the actual line, that the line in the MySQL DB would have in the serverside table.
We just found out that the index we were searching for is "3" - the one that is assigned to our "n". So we assign this to the rst variable.
At this point we know which line/index in the serverside table we need and we have stored in a variable. Therefor there is no need in going on with our loop anymore, as we suppose there is only
1 matching line. As we have found it, we can stop searching the through the following lines -> we stop the loop with
pawn Код:
break;
pawn Код:
return rst;
Код:
I have found a result, it is X" with X = whichever number is stored in rst -> -1 in case there is no found matching line!