【问题标题】:Populating external foreach loop array with SQL results使用 SQL 结果填充外部 foreach 循环数组
【发布时间】:2012-01-22 04:30:01
【问题描述】:

我是 php newbee,我正在尝试使用一系列 foreach 和 SQL 结果填充 3 个不同的数组。该代码实际上有效,但我只打印最后一个结果。不知何故,我没有增加数组,而是一遍又一遍地写它。问题是,如何增加它们?任何帮助都非常感谢!

        $online = array();//armazena online
    $ocupado = array();//armazena ocupado
    $offline = array();//armazena offline

$atendentes = mysql_query("SELECT nome FROM atendentes ORDER BY nome")or die(mysql_error());
while ($row = mysql_fetch_assoc($atendentes)) {/// Pegar atendentes
    $atendentedb = array($row["nome"]);
}

foreach ($atendentedb as $atendente) {// LOOP SELECAO DAS ATENDENTES; VERIFICAR CADA UMA

    $names  = modWhosonlineCustom::getOnlineUserNames();//pega o nome de quem esta online agora

    foreach ($names as $name){//DA UM LOOP, EM QUEM ESTA ONLINE E MARCA 

    //*****************************'ACENDER' se a atendente esta online
    $att = $name->username;
    $att = strtolower($att);

    if ($atendente == $att){//esta atendente esta online
        $att_online = 'yes';
        }//esta atendente esta online
    if ($atendente != $att){//esta nao atendente esta online
        $att_online = 'no';
        }//esta atendente nao esta online
    //******************************************************************

    }//loop foreach quem esta online

    if ($att_online == 'yes'){//se atendente online
    $status = mysql_query("SELECT status FROM atendentes WHERE nome = '$atendente'")or die(mysql_error());
    while ($row = mysql_fetch_assoc($status)) {
    $statusdb = $row["status"];
    }
        //**************************** VERIFICA O STATUS
        if ($statusdb == 'disponivel'){
        //*******************************
        $descricao = mysql_query("SELECT hp_online FROM atendentes WHERE nome = '$atendente'")or die(mysql_error());
        while ($row = mysql_fetch_assoc($descricao)) {
        $online[] = $row['hp_online'];
            }
        }// se o status é disponivel

                //*********************************  OCUPADOS           
                if ($statusdb == 'ocupado'){
                $descricao = mysql_query("SELECT hp_busy FROM atendentes WHERE nome = '$atendente'")or die(mysql_error());
                while ($row = mysql_fetch_assoc($descricao)) {
                $ocupado[] = $row['hp_busy'];
                }
                    }// se o status é ocupado
    }//atendente = yes  

    if ($att_online != 'yes'){//se estiver offline
        $descricao = mysql_query("SELECT hp_offline, horario FROM atendentes WHERE nome = '$atendente'")or die(mysql_error());
        while ($row = mysql_fetch_assoc($descricao)) {
        $offline[] = $row['hp_offline'];
        $offline[] = $row['horario'];
                }
        }//se att nao é online

}// loop foreach

【问题讨论】:

    标签: php sql arrays foreach while-loop


    【解决方案1】:

    在 while 循环中获取时,使用 [] 语法将值附加到数组中。否则,您只是在每次循环迭代中覆盖相同的值。

    $atendentedb = array();
    while ($row = mysql_fetch_assoc($atendentes)) {/// Pegar atendentes
        $atendentedb[] = array($row["nome"]);
        //--------^^^^
    }
    
    // Later
    $statusdb = array();
    while ($row = mysql_fetch_assoc($status)) {
      $statusdb[] = $row["status"];
      //-------^^
    }
    

    如果我在您的代码中遗漏了其他内容,您也应该更改这些内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 2020-09-11
      相关资源
      最近更新 更多