SA-MP Forums Archive
List a foreach() or while() in one line - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: List a foreach() or while() in one line (/showthread.php?tid=201445)



List a foreach() or while() in one line - hanzen - 21.12.2010

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?


Re: List a foreach() or while() in one line - Rachael - 21.12.2010

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;
            }
        }
    }



Re: List a foreach() or while() in one line - hanzen - 21.12.2010

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?


Re: List a foreach() or while() in one line - Rachael - 21.12.2010

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.