#1

As I rewrited MySQL from R6-2 to R7, I got a problem now with TOP10. For example, there is one registered player with name ABC with 100$, so the TOP10 should look like this:

Quote:

1. ABC ($100)
2. There is no registered player
3. There is no registered player
4. There is no registered player
5. There is no registered player
6. There is no registered player
7. There is no registered player
8. There is no registered player
9. There is no registered player
10. There is no registered player

But the problem is that the TOP looks like this:

Quote:

1. ABC ($100)
2. ABC ($100)
3. ABC ($100)
4. ABC ($100)
5. ABC ($100)
6. ABC ($100)
7. ABC ($100)
8. ABC ($100)
9. ABC ($100)
10. ABC ($100)

The code:

pawn Код:
PUB::GetStatistic(playerid, Field[], Command[], bool: toStrval)
{
    new szQuery[88];
    format(szQuery, 87, "SELECT `Name`, `%s` FROM `players` ORDER BY `%s` %s LIMIT 10", Field, Field, Command);
    mysql_function_query(iHandle, szQuery, true, "GetServerStatistic", "dssdd", playerid, Field, Command, Item, toStrval);
}

pawn Код:
PUB::GetServerStatistic(playerid, Field[], Command[], bool: toStrval)
{
    new
        szResult[1100],
        szField [21],
        pName   [24],
        iFields,
        iRows;
       
    >>>>>>>>>for (new i; i != 10; ++i)
    {
        cache_get_data(iRows, iFields);
        if (iRows)
        {
            cache_get_row(0, 0, pName);
            cache_get_row(0, 1, szField);
            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);
            }
        }<<<<<<<<<<<<
        else
        {
            format(szResult, 1100, "%s{FF0000}%d. {33AA33}Nлra\n", szResult, i + 1);
        }
    }
          ........
I think the problem is where I put >><<, but I dont know, how to fix it.

Any solutions? Thx.
Reply
#2

Examine closely:
pawn Код:
cache_get_row(0, 0, pName);
cache_get_row(0, 1, szField);
Reply
#3

So I should use cache_get_field?
Reply
#4


No, you're just fetching the same row over and over again. Of course you're gonna get ten times the same result. Replace the first 0 with your index: i in both statements.
Reply
#5

This should be not facepalm, its should be faceplant.. Thank you so much.
Reply
#6

Sorry for bumping, but I have tested in and it isnt working at it should. The dialog looks like this:

1. ABC ($100)
2. NULL (0)
3. NULL (0)
4. NULL (0)
5. NULL (0)
6. NULL (0)
7. NULL (0)
8. NULL (0)
9. NULL (0)
10. NULL (0)

Since it should

1. ABC ($100)
2. There is no registered player
3. There is no registered player
4. There is no registered player
5. There is no registered player
6. There is no registered player
7. There is no registered player
8. There is no registered player
9. There is no registered player
10. There is no registered player
Reply
#7

I cannot continue before I mention this. Things like this:
pawn Код:
format(szResult, 1100, "%s{FF0000}%d. {33AA33}%s ({FF0000}%d{33AA33})\n", szResult, i + 1, pName, strval(szField));
... are SLOW oh SLOW. Even something like this:
pawn Код:
new temp[128];
format(temp, sizeof(temp), "{FF0000}%d. {33AA33}%s ({FF0000}%d{33AA33})\n", i + 1, pName, strval(szField));
strcat(szResult, temp);
... are FASTER when your strings grow big!

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);
    }
}
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:
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;
}
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.
Reply
#8

Why do you want to list 9 of those strings? Why not just list the number #1 ?

You are obviously soon going to have more than 10 players registered, so why bother doing this extra code? The first 10 people registered are only going to see it - and then it's a whole waste of code.

So instead of:

1.) My Name ($100)
2) shit
--
9) shit

Just:
1.) My Name ($100)

--nothing here unless more players are registered--


That would be easier, and faster.
Reply
#9

Quote:
Originally Posted by AndreT
Посмотреть сообщение
I cannot continue before I mention this. Things like this:
pawn Код:
format(szResult, 1100, "%s{FF0000}%d. {33AA33}%s ({FF0000}%d{33AA33})\n", szResult, i + 1, pName, strval(szField));
... are SLOW oh SLOW. Even something like this:
pawn Код:
new temp[128];
format(temp, sizeof(temp), "{FF0000}%d. {33AA33}%s ({FF0000}%d{33AA33})\n", i + 1, pName, strval(szField));
strcat(szResult, temp);
... are FASTER when your strings grow big!

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);
    }
}
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:
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;
}
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.
Thank you so much :> And thank you too Pizzy
Reply
#10

Quote:
Originally Posted by Pizzy
Посмотреть сообщение
Why do you want to list 9 of those strings? Why not just list the number #1 ?

You are obviously soon going to have more than 10 players registered, so why bother doing this extra code? The first 10 people registered are only going to see it - and then it's a whole waste of code.

So instead of:

1.) My Name ($100)
2) shit
--
9) shit

Just:
1.) My Name ($100)

--nothing here unless more players are registered--


That would be easier, and faster.
Indeed, sir. I thought about it for the quarter of a second, but being used to how in certain cases I fill CSTL vectors with "dummy" data as I call it to avoid running a number of extra checks when something gets modified, I resorted to pointing out to what he actually needs.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)