display php nested array results continuously in 2 column table -
display php nested array results continuously in 2 column table -
i have nested array grouping 3 other arrays: $online, $busy , $offline, in order display results in order.
$conteudo = array($online, $ocupado, $offline); the disired result table in 2 columns display results in continuous flow this:
online1 | online2 online3 | busy1 busy2 | offline1 offline2| offline3 offline4| i've been trying lots of foreach loops , playing around changing html tags, closiest can arrive of desired result this:
<table width='100%' cellpadding='5' border="1"> <?php $i = 1; //contador de colunas $max_colunas = 2; // numero de colunas foreach ($conteudo $row) { echo "<tr>"; foreach ($row $col) { foreach ($col $cell) { if ($i == 1) { echo "<td>" . $cell . "</td>"; } elseif ($i == $max_colunas){ echo "<td>" . $cell . "</td></tr>"; $i = 0; } else { echo "<td>" . $cell . "</td>"; } $i++; } } echo "</tr>"; } this code output table this:
onine1 | online2 |online3 busy1 | busy2 | offline1|offline2 |offline3|offline4 i can't findout why ignores completly $max_colunas, seems prints elements within array in row.
if remove lines
echo "<tr>"; echo "</tr>"; from begining , end of foreach output in row this:
onine1 | online2 |online3 | busy1 | busy2 |offline1|offline2 |offline3|offline4 :(( sugestions desired output format appreciated, give thanks you!
edited on 17/01:
this how i'm getting arrays from:
//group people $online = array(); //group online $ocupado = array(); //group ocupado $offline = array(); //group offline //select people grouping $atendentes = mysql_query("select nome atendentes order nome") or die(mysql_error()); $atendentedb = array(); //put selected people in array while ($row = mysql_fetch_assoc($atendentes)) { $atendentedb[] = array('nome' => $row["nome"], 'online' => false); } //take people online , check in selected people $names = modwhosonlinecustom::getonlineusernames(); foreach ($names $name): //foreach ($atendentedb $atendente): for($i = 0; $i < count($atendentedb); $i++): $att = strtolower($name->username); if ($atendentedb[$i]['nome'] == $att): $atendentedb[$i]['online'] = true; break; endif; endfor; endforeach; //check each selected people foreach ($atendentedb $atendente) : //save temporary info $online_ = $atendente['online']; $nome_ = $atendente['nome']; //if selected people online if ($online_) : //take status show $status = mysql_query("select status atendentes nome = '$nome_' limit 1") or die(mysql_error()); while ($row = mysql_fetch_assoc($status)): $statusdb = $row["status"]; endwhile; //verify , save deppending on status switch ($statusdb): //if online case "disponivel": $descricao = mysql_query("select hp_online atendentes nome = '$nome_' limit 1") or die(mysql_error()); while ($row = mysql_fetch_assoc($descricao)): $online[] = array('info'=>$row['hp_online']); endwhile; break; //if busy case "ocupado": $descricao = mysql_query("select hp_busy atendentes nome = '$nome_' limit 1") or die(mysql_error()); while ($row = mysql_fetch_assoc($descricao)): $ocupado[] = array('info'=>$row['hp_busy']); endwhile; break; endswitch; //if offline else: $descricao = mysql_query("select hp_offline, horario atendentes nome = '$nome_' limit 1") or die(mysql_error()); while ($row = mysql_fetch_assoc($descricao)): $offline[] = array('info'=>$row['hp_offline'], 'horario'=>$row['horario']); endwhile; endif; endforeach; edited
so after next help instructions daverandom got code, drop away right format, except "misterious" behavor results coming array $offline, displaying in "block" (all cells in row, or cells in collumn) while other arrays displaying perfectly(??).
//group people $online = $ocupado = $offline = array(); //select people grouping $query = "select nome, status, hp_online, hp_busy, hp_offline, horario atendentes order nome"; $atendentes = mysql_query($query) or die(mysql_error()); $atendentedb = array(); // set selected people in array while ($row = mysql_fetch_assoc($atendentes)) { $atendentedb[strtolower($row['nome'])] = array_merge($row, array('online' => false)); } //take people online , check in selected people $names = modwhosonlinecustom::getonlineusernames(); foreach ($names $name) { $uname = strtolower($name->username); if (isset($atendentedb[$uname])) $atendentedb[$uname]['online'] = true; } //check each selected people foreach ($atendentedb $name => $atendente) { //if selected people online if ($atendente['online']) { //verify , save deppending on status switch ($atendente['status']) { //if online case 'disponivel': $atendentedb[$name]['info'] = $online[] = $atendente['hp_online']; break; //if busy case 'ocupado': $atendentedb[$name]['info'] = $ocupado[] = $atendente['hp_busy']; break; } //if offline } else { $atendentedb[$name]['info'] = $offline[] = $atendente['hp_offline']; $atendentedb[$name]['info'] = $offline[] = $atendente['horario']; } } //*******display results $conteudo = array_merge($online, $ocupado, $offline); $max_colunas = 2; // numero de colunas // start table echo '<table width="100%" cellpadding="5" border="1">'."\n"; // loop objects ($i = 0, $j = 0; isset($conteudo[$i]); $i++) { if ($j == 0) { // output origin of row echo " <tr>\n"; } // output info cell echo " <td>$conteudo[$i]</td>\n"; if (++$j >= $max_colunas) { // output end of row , reset cell counter echo " </tr>\n"; $j = 0; } } if ($j) { // may end incomplete row @ end, pad empty cells // , close row while ($j++ < $max_colunas) { echo " <td></td>\n"; } echo " </tr>\n"; } // close table echo "</table>";
i first thing here "flatten" array - having multiple dimensions makes lot more complicated needs be. rather creating $conteudo this:
$conteudo = array($online, $ocupado, $offline); ...do instead:
$conteudo = array_merge($online, $ocupado, $offline); then can this:
$max_colunas = 2; // numero de colunas // start table echo '<table width="100%" cellpadding="5" border="1">'."\n"; // loop objects ($i = 0, $j = 0; isset($conteudo[$i]); $i++) { if ($j == 0) { // output origin of row echo " <tr>\n"; } // output info cell echo " <td>$conteudo[$i]</td>\n"; if (++$j >= $max_colunas) { // output end of row , reset cell counter echo " </tr>\n"; $j = 0; } } if ($j) { // may end incomplete row @ end, pad empty cells // , close row while ($j++ < $max_colunas) { echo " <td></td>\n"; } echo " </tr>\n"; } // close table echo "</table>"; see working
edit
try code generating arrays. note construction of output arrays has been altered fit code sample above - if utilize info anywhere else in script, need modify code well. have modified there 1 database query, seem required. have modified $atendentedb holds user data, including status , info keys, , rows contain horario key.
because of fact input arrays contained more info ones created code will, code may need farther modification - seek out , see how on.
//group people $online = $ocupado = $offline = array(); //select people grouping $query = "select nome, status, hp_online, hp_busy, hp_offline, horario atendentes order nome"; $atendentes = mysql_query($query) or die(mysql_error()); $atendentedb = array(); // set selected people in array while ($row = mysql_fetch_assoc($atendentes)) { $atendentedb[strtolower($row['nome'])] = array_merge($row, array('online' => false)); } //take people online , check in selected people $names = modwhosonlinecustom::getonlineusernames(); foreach ($names $name) { $uname = strtolower($name->username); if (isset($atendentedb[$uname])) $atendentedb[$uname]['online'] = true; } //check each selected people foreach ($atendentedb $name => $atendente) { //if selected people online if ($atendente['online']) { //verify , save deppending on status switch ($atendente['status']) { //if online case 'disponivel': $atendentedb[$name]['info'] = $online[] = $atendente['hp_online']; break; //if busy case 'ocupado': $atendentedb[$name]['info'] = $ocupado[] = $atendente['hp_busy']; break; } //if offline } else { $atendentedb[$name]['info'] = $offline[] = $atendente['hp_offline'].' '.$atendente['horario']; } } php arrays table nested
Comments
Post a Comment