30.06.2012, 20:36
I cannot continue before I mention this. Things like this:
... are SLOW oh SLOW. Even something like this:
... are FASTER when your strings grow big!
Now please, examine closely what you are doing in your loop there.
To simplify understanding, I wrote some comments.
Now lets look at what needs to be done.
1. Get the amount of rows returned by the query.
2. Iterate through the rows that the query returned and fill the szResult string accordingly.
3. If we did not get 10 rows from the query, fill the rest of the statistics table with "Account not registered" lines.
To achieve your wanted outcome, the code has to look something like this:
I hope this helps you out a little bit. It does not give you the whole working code, but perhaps an idea on how to solve the logic of the code.
pawn Код:
format(szResult, 1100, "%s{FF0000}%d. {33AA33}%s ({FF0000}%d{33AA33})\n", szResult, i + 1, pName, strval(szField));
pawn Код:
new temp[128];
format(temp, sizeof(temp), "{FF0000}%d. {33AA33}%s ({FF0000}%d{33AA33})\n", i + 1, pName, strval(szField));
strcat(szResult, temp);
Now please, examine closely what you are doing in your loop there.
pawn Код:
for (new i; i != 10; ++i)
{
cache_get_data(iRows, iFields); // Get the amount of fields and rows returned by the query IN THE LOOP. WHY!?
if (iRows) // Check the row count.
{
cache_get_row(0, 0, pName); // Extract the name from the FIRST row. In ALL caes, the first row.
cache_get_row(0, 1, szField); // The same for this line. It would be a bit better if you used i instead of 0.
if (toStrval)
{
format(szResult, 1100, "%s{FF0000}%d. {33AA33}%s ({FF0000}%d{33AA33})\n", szResult, i + 1, pName, strval(szField));
}
else
{
format(szResult, 1100, "%s{FF0000}%d. {33AA33}%s ({FF0000}%s{33AA33})\n", szResult, i + 1, pName, szField);
}
// Please optimize the lines above as I suggeste!
}
else
{
format(szResult, 1100, "%s{FF0000}%d. {33AA33}Nлra\n", szResult, i + 1);
}
}
Now lets look at what needs to be done.
1. Get the amount of rows returned by the query.
2. Iterate through the rows that the query returned and fill the szResult string accordingly.
3. If we did not get 10 rows from the query, fill the rest of the statistics table with "Account not registered" lines.
To achieve your wanted outcome, the code has to look something like this:
pawn Код:
PUB::GetServerStatistic(playerid, Field[], Command[], bool: toStrval)
{
new rows, fields;
cache_get_data(rows, fields);
for(new i = 0; i != rows; i++)
{
cache_get_row(i, 0, pName);
cache_get_row(i, 1, szField);
// Formatting code here.
}
if(rows != 10)
{
// If the query did not return 10 rows, fill in the dialog with empty lines ("There is no registered player")
for(new i = 0; i != (10 - rows); i++)
{
// append "There is no registered player\n"
}
}
return 1;
}