2011-12-16 57 views
1

我是一個php newbee,我試圖用一系列的foreach和SQL結果填充3個不同的數組。代碼實際上起作用,但我只打印最後一個結果。不知何故,我不是遞增數組,而是一遍又一遍地寫。問題是,如何增加它們?任何幫助真的很感激!用SQL結果填充外部foreach循環數組

 $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 

回答

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"]; 
    //-------^^ 
} 

如果我錯過了代碼中的其他人,也應該更改這些代碼。

+0

非常感謝邁克爾,解決了這個問題:))) – Adry 2012-01-16 18:00:31