While loop problem
#1

Hello, thanks for reading this.
I'm having a few problems with a while loop to load cars in my script. Here is my code:
pawn Код:
forward loadCars_query();
public loadCars_query()
{
    new rows, fields;
    new i = -1;
    cache_get_data(rows, fields);
    if(rows)
    {
        while(i < rows)
        {
            i++;
            new temp[4], id;
            printf("i | rows : %d | %d", i, rows);
            cache_get_field_content(i, "ID", temp), id = strval(temp);
        }
    }
}
As you can see, i++ should be increased with every iteration. However, the log prints this out (the server hangs, too):
Quote:

i | rows : 0 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2
i | rows : 1 | 2

I'm not sure what I'm doing wrong, I hope one of you can help me.
Reply
#2

I'm not sure what the problem is but try using a for loop?

pawn Код:
forward loadCars_query();
public loadCars_query()
{
    new rows, fields;
    cache_get_data(rows, fields);
    if(rows)
    {
        for(new i = -1; i < rows; i++)
        {
            new temp[4], id;
            printf("i | rows : %d | %d", i, rows);
            cache_get_field_content(i, "ID", temp), id = strval(temp);
        }
    }
}
I believe a for loop is faster anyway.
Reply
#3

well while loop is faster and do PWN have while loop OMG! i thought it was only in BlueJ(JAVA)
EDIT:and i too think that the upper for loop gona work because i do think that loop's dont support negative value's coz my teacher Once Cutted my whole program due to it in my exams ;( i lost my 15 marks due to it could be your while loop problem too
Reply
#4

I only used -1 to see if placing i++ at the beginning would make the code work. I'll try that for loop in a second, thanks.
Reply
#5

I tried this:
pawn Код:
forward loadCars_query();
public loadCars_query()
{
    new rows, fields;
    cache_get_data(rows, fields);
    if(rows)
    {
        for(new i = 0; i < rows; i++)
        {
            new temp[4], id;
            printf("i | rows : %d | %d", i, rows);
            cache_get_field_content(i, "ID", temp), id = strval(temp);
        }
    }
}
And just get the same output.
Reply
#6

The cache_get_field_content is from BlueG's SQL plugin (see here: https://sampforum.blast.hk/showthread.php?tid=337810)

However, I used cache_get_row rather than cache_get_field_content (which should both do the same thing, but I'm not sure why they didn't, and it worked fine. Here is the code I settled with:
pawn Код:
forward loadCars_query();
public loadCars_query()
{
    new
        rows,
        fields;
       
    cache_get_data(rows, fields);
   
    if(rows)
    {
        new
            temp[5],
            i,
            id;
           
        while(i < rows)
        {
            cache_get_row(i, 0, temp);
            id = strval(temp);
            printf("i | rows : %d | %d", id, i, rows);
            i++;
        }
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)