List a foreach() or while() in one line
#1

Hello..

Is it possible to list several rows in a query in one line?

If I want like /admins (example) and it to list all the admins in one line? Is that possible?
Reply
#2

pawn Код:
CMD:admins(playerid, params[])
{
    new
        bool:any,
        namestring[24],
        adminstring[256] = "Admins: ";
    ;
    any = false;
    foreach(Character, i)
    {
        if(PlayerInfo[i][pAdmin])
        {
            if(any == true)
            {
                strins(adminstring,", ",strlen(adminstring));
            }
            GetPlayerName(i,namestring,24);
            strins(adminstring,namestring,strlen(adminstring));
            any = true;
        }
    }

    if(!any)
    {
        strins(adminstring,"none.",strlen(adminstring));
    }

    SendClientMessage(playerid,0xAFAFAFAA,adminstring);
    return 1;
}
note: this code uses <zcmd> and <foreach>

if you do not have <foreach> included in your script, replace the foreach(Character, i) line, and add another level to the statement
pawn Код:
for(new i; i < MAX_PLAYERS;i++)
    {
        if(IsPlayerConnected(i))
        {
             if(PlayerInfo[i][pAdmin])
            {
                if(any == true)
                {
                    strins(adminstring,", ",strlen(adminstring));
                }
                GetPlayerName(i,namestring,24);
                strins(adminstring,namestring,strlen(adminstring));
                any = true;
            }
        }
    }
Reply
#3

Quote:
Originally Posted by Rachael
Посмотреть сообщение
pawn Код:
CMD:admins(playerid, params[])
{
    new
        bool:any,
        namestring[24],
        adminstring[256] = "Admins: ";
    ;
    any = false;
    foreach(Character, i)
    {
        if(PlayerInfo[i][pAdmin])
        {
            if(any == true)
            {
                strins(adminstring,", ",strlen(adminstring));
            }
            GetPlayerName(i,namestring,24);
            strins(adminstring,namestring,strlen(adminstring));
            any = true;
        }
    }

    if(!any)
    {
        strins(adminstring,"none.",strlen(adminstring));
    }

    SendClientMessage(playerid,0xAFAFAFAA,adminstring);
    return 1;
}
note: this code uses <zcmd> and <foreach>

if you do not have <foreach> included in your script, replace the foreach(Character, i) line, and add another level to the statement
pawn Код:
for(new i; i < MAX_PLAYERS;i++)
    {
        if(IsPlayerConnected(i))
        {
             if(PlayerInfo[i][pAdmin])
            {
                if(any == true)
                {
                    strins(adminstring,", ",strlen(adminstring));
                }
                GetPlayerName(i,namestring,24);
                strins(adminstring,namestring,strlen(adminstring));
                any = true;
            }
        }
    }
Yeah thanks, got it working. But if I want to list like ALL players that's registrered from MySQL (all admins) and I make the correct query and such. Could i do like 5 names per string? Without using LIMIT?
Reply
#4

yes, just put in a line that checks the length of the string every loop, and if it is greater than, say 128, print it and set any = false.
Don't forget to check the length of the string after the loop, and print the remainder if it is more than zero.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)