Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm a php newbee and I'm trying to populate 3 diferent arrays with a serie of foreach and SQL results. The code actually works, but I keep printing only the last result. Somehow I'm not incrementing the arrays but just writing it over and over. The question is, how to incremente them? Any help is really appreciate!

        $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
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

When fetching in a while loop, use the [] syntax to append values to an array. Otherwise, you are just overwriting the same value on each loop iteration.

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

If I missed others in your code, you should change those as well.

share|improve this answer
 
Thanks a lot Michael, that solves the problem :))) –  Adry Jan 16 '12 at 18:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.