Portal SAMP
[Pedido] Loop em MySQL como fazer - Versão de Impressão

+- Portal SAMP (https://portalsamp.com)
+-- Fórum: SA-MP (https://portalsamp.com/forumdisplay.php?fid=5)
+--- Fórum: Área de suporte (https://portalsamp.com/forumdisplay.php?fid=6)
+--- Tópico: [Pedido] Loop em MySQL como fazer (/showthread.php?tid=1118)



Loop em MySQL como fazer - Woorz - 21/04/2021

Olá Família!

Tenho uma dúvida de como posso fazer um loop em MySQL.

Exemplo:
Irá consultar a Coluna "contas" e "facções".

Ele irá retornar todos os membros offline e online desta facção no /facção após selecionar a facção id específica.


RE: Loop em MySQL como fazer - Vel - 21/04/2021

Código:
ShowFactionMembersList(playerid, factionid) {
  new list[4096];

  mysql_query(MYSQL_DEFAULT_HANDLE, "SELECT `fid`, `nome` FROM `contas`");

  for (new i; i < cache_num_rows(); i++) {
    new fid = -1;

    if ((cache_get_value_int(i, 0, fid), fid) == factionid) {
      new name[MAX_PLAYER_NAME + 1];

      cache_get_value(i, 1, name);

      format(list, sizeof(list), "%s%s\t%s\n", list, name, GetPlayerIDByName(name) == INVALID_PLAYER_ID ? "Offline" : "Online");
    }
  }

  ShowPlayerDialog(playerid, 0, DIALOG_STYLE_TABLIST, "Membros", list, "OK", "");
}

GetPlayerIDByName(const name[]) {
  new playerid = INVALID_PLAYER_ID;
  return sscanf(name, "r", playerid), playerid;
}



RE: Loop em MySQL como fazer - Woorz - 21/04/2021

(21/04/2021 10:36)Vel Escreveu:
Código:
ShowFactionMembersList(playerid, factionid) {
  new list[4096];

  mysql_query(MYSQL_DEFAULT_HANDLE, "SELECT `fid`, `nome` FROM `contas`");

  for (new i; i < cache_num_rows(); i++) {
    new fid = -1;

    if ((cache_get_value_int(i, 0, fid), fid) == factionid) {
      new name[MAX_PLAYER_NAME + 1];

      cache_get_value(i, 1, name);

      format(list, sizeof(list), "%s%s\t%s\n", list, name, GetPlayerIDByName(name) == INVALID_PLAYER_ID ? "Offline" : "Online");
    }
  }

  ShowPlayerDialog(playerid, 0, DIALOG_STYLE_TABLIST, "Membros", list, "OK", "");
}

GetPlayerIDByName(const name[]) {
  new playerid = INVALID_PLAYER_ID;
  return sscanf(name, "r", playerid), playerid;
}


Opa irmão, valeu pela reposta. Então está dando estes erros abaixo.
Estou testando assim:

Código:
Dialog:FactionsList2(playerid, response, listitem, inputtext[])
{
if (response)
{
        ShowFactionMembersList(playerid, listitem);
    }
    return 1;
}


// ESSES ERROS AQUI NO ShowFactionMembersList //
/*
error 017: undefined symbol "cache_get_value_int"
error 017: undefined symbol "cache_get_value"
error 001: expected token: "-string end-", but found "-identifier-"
*/

Uso MYSQL R39-3
ShowFactionMembersList(playerid, factionid)
{
    new list[4096];

    mysql_tquery(g_iHandle, "SELECT `Faction`, `Character` FROM `characters`");

    for (new i; i < cache_num_rows(); i++)
    {
        new fid = -1;

        if ((cache_get_value_int(i, 0, fid), fid) == factionid)
        {
            new name[MAX_PLAYER_NAME + 1];

            cache_get_value(i, 1, name);

            format(list, sizeof(list), "%s%s\t%s\n", list, name, GetPlayerIDByName(name) == INVALID_PLAYER_ID ? "Offline" : "Online");
        }
    }
    Dialog_Show(playerid, FactionsList2, DIALOG_STYLE_TABLIST_HEADERS, "Membros", list, "Fechar", "");
}

GetPlayerIDByName(const name[])
{
    new playerid = INVALID_PLAYER_ID;
    return sscanf(name, "r", playerid), playerid;
}



RE: Loop em MySQL como fazer - Vel - 21/04/2021

(21/04/2021 16:35)Woorz Escreveu:
(21/04/2021 10:36)Vel Escreveu:
Código:
ShowFactionMembersList(playerid, factionid) {
  new list[4096];

  mysql_query(MYSQL_DEFAULT_HANDLE, "SELECT `fid`, `nome` FROM `contas`");

  for (new i; i < cache_num_rows(); i++) {
    new fid = -1;

    if ((cache_get_value_int(i, 0, fid), fid) == factionid) {
      new name[MAX_PLAYER_NAME + 1];

      cache_get_value(i, 1, name);

      format(list, sizeof(list), "%s%s\t%s\n", list, name, GetPlayerIDByName(name) == INVALID_PLAYER_ID ? "Offline" : "Online");
    }
  }

  ShowPlayerDialog(playerid, 0, DIALOG_STYLE_TABLIST, "Membros", list, "OK", "");
}

GetPlayerIDByName(const name[]) {
  new playerid = INVALID_PLAYER_ID;
  return sscanf(name, "r", playerid), playerid;
}


Opa irmão, valeu pela reposta. Então está dando estes erros abaixo.
Estou testando assim:

Código:
Dialog:FactionsList2(playerid, response, listitem, inputtext[])
{
if (response)
{
        ShowFactionMembersList(playerid, listitem);
    }
    return 1;
}


// ESSES ERROS AQUI NO ShowFactionMembersList //
/*
error 017: undefined symbol "cache_get_value_int"
error 017: undefined symbol "cache_get_value"
error 001: expected token: "-string end-", but found "-identifier-"
*/

Uso MYSQL R39-3
ShowFactionMembersList(playerid, factionid)
{
    new list[4096];

    mysql_tquery(g_iHandle, "SELECT `Faction`, `Character` FROM `characters`");

    for (new i; i < cache_num_rows(); i++)
    {
        new fid = -1;

        if ((cache_get_value_int(i, 0, fid), fid) == factionid)
        {
            new name[MAX_PLAYER_NAME + 1];

            cache_get_value(i, 1, name);

            format(list, sizeof(list), "%s%s\t%s\n", list, name, GetPlayerIDByName(name) == INVALID_PLAYER_ID ? "Offline" : "Online");
        }
    }
    Dialog_Show(playerid, FactionsList2, DIALOG_STYLE_TABLIST_HEADERS, "Membros", list, "Fechar", "");
}

GetPlayerIDByName(const name[])
{
    new playerid = INVALID_PLAYER_ID;
    return sscanf(name, "r", playerid), playerid;
}

Troca "cache_get_value_int" por "cache_get_row_int" e "cache_get_value" por "cache_get_row".



RE: Loop em MySQL como fazer - Woorz - 22/04/2021

Também não foi meu mano Sad.

Tô tentando assim:
Código:
CMD:testandolista(playerid, params[])
{
    CheckFactionL(playerid, ID DA FACCAO AQUI); //FactionData[factionid][factionID] ou manual
    return 1;
}

stock CheckFactionL(playerid, factionid1)
{
    new
        query[200];

    format(query, sizeof(query), "SELECT Character FROM characters WHERE Faction = '%d'", factionid1);
    mysql_tquery(g_iHandle, query, "CheckFactionL2", "dd", playerid, factionid1);
    return 1;
}

forward CheckFactionL2(extraid, CheckFactionL);
public CheckFactionL2(extraid, CheckFactionL)
{

    static
        rows,
        fields;

    cache_get_data(rows, fields, g_iHandle);

    if (!rows)
        return SendErrorMessage(extraid, "Não há membros na facção ID "%d".", CheckFactionL);

    static
        string[1024],
        string2[100],
        desc[128],
        count;

    string[0] = 0;

    for (new i = 0; i < rows; i ++)
    {
        cache_get_field_content(i, "Character", desc, g_iHandle);

        format(string, sizeof(string), "%s%s\n", string, desc);
        count++;
    }
    format(string, sizeof(string), "Nome\n %s", string);
    strcat(string, string2);

    format(desc, sizeof(desc), "Total de membros: %d", count);
    Dialog_Show(extraid, CheckFactionL3, DIALOG_STYLE_TABLIST_HEADERS, desc, string, "Fechar", "");
    return 1;
}

Porém fica ele fica retornando: Não há membros na facção ID....