在两列表中连续显示php嵌套数组结果


Display php nested array results continuously in two column table

我有一个嵌套数组,将其他三个数组分组:$online、$busy和$offline,以便按此顺序显示结果。

$conteudo = array($online, $ocupado, $offline);

所需的结果将是一个两列的表格,以连续流显示结果,如下所示:

online1 | online2
online3 | busy1
busy2   | offline1
offline2| offline3
offline4|

我已经尝试了很多foreach循环,并尝试更改html标签,但最接近预期结果的是:

<table width='100%' cellpadding='5' border="1">
<?php
$i = 1; //contador de colunas
$max_colunas = 2; // numero de colunas
foreach ($conteudo as $row) {
    echo "<tr>";
    foreach ($row as $col) {
        foreach ($col as $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>"; 
}   

此代码将输出如下表:

 onine1  | online2 |online3
 busy1   | busy2   |
 offline1|offline2 |offline3|offline4

我不知道为什么它完全忽略$max_colunas,似乎它将数组中的所有元素打印成一行。

如果我删除行:

 echo "<tr>";
 echo "</tr>";

foreach的开始和结束,它将像这样一行输出所有内容:

onine1  | online2 |online3 | busy1 | busy2 |offline1|offline2 |offline3|offline4

任何获得所需输出格式的建议都将不胜感激。


编辑日期:17/01:

这就是我获取阵列的方式:

//group people
$online = array(); //group online
$ocupado = array(); //group ocupado
$offline = array(); //group offline
//select people to group
$atendentes = mysql_query("SELECT nome FROM atendentes ORDER BY 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 now and check in selected people
$names = modWhosonlineCustom::getOnlineUserNames();
foreach ($names as $name):
    //foreach ($atendentedb as $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 as $atendente) :
    //save temporary data
    $online_ = $atendente['online'];
    $nome_   = $atendente['nome'];  
    //if selected people online
    if ($online_) :
        //take status to show
        $status = mysql_query("SELECT status FROM atendentes WHERE nome = '$nome_' LIMIT 1") or die(mysql_error());
        while ($row = mysql_fetch_assoc($status)):
            $statusdb = $row["status"];
        endwhile;
        //verify and save deppending on status
        switch ($statusdb):
            //if online
            case "disponivel":
                $descricao = mysql_query("SELECT hp_online FROM atendentes WHERE 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 FROM atendentes WHERE 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 FROM atendentes WHERE 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;

已编辑

因此,在遵循了DaveRandom的帮助说明后,我得到了这段代码,这与正确的格式相差甚远,除了来自数组$offline的结果的"神秘"行为之外,其他数组都在"块"中显示(一行中的所有单元格或一列中的所有单元),而其他数组则完美地显示(??)。

   //group people
   $online = $ocupado = $offline = array();
   //select people to group
   $query = "SELECT nome, status, hp_online, hp_busy, hp_offline, horario
      FROM atendentes
      ORDER BY nome";
  $atendentes = mysql_query($query) or die(mysql_error());
  $atendentedb = array();
  // put selected people in array
  while ($row = mysql_fetch_assoc($atendentes)) {
  $atendentedb[strtolower($row['nome'])] = array_merge($row, array('online' => FALSE));
  }
  //take people online now and check in selected people
  $names = modWhosonlineCustom::getOnlineUserNames();
  foreach ($names as $name) {
  $uname = strtolower($name->username);
  if (isset($atendentedb[$uname])) $atendentedb[$uname]['online'] = TRUE;
  }
  //check each selected people
  foreach ($atendentedb as $name => $atendente) {
  //if selected people online
  if ($atendente['online']) {
 //verify and 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 the table
  echo '<table width="100%" cellpadding="5" border="1">'."'n";
  // Loop all the objects
  for ($i = 0, $j = 0; isset($conteudo[$i]); $i++) {
  if ($j == 0) {
  // Output the beginning of a row
  echo "  <tr>'n";
  }
  // Always output a data cell
  echo "    <td>$conteudo[$i]</td>'n";
  if (++$j >= $max_colunas) {
  // Output the end of a row and reset the cell counter
  echo "  </tr>'n";
  $j = 0;
  }
  }
  if ($j) {
  // We may end up with an incomplete row at the end, so pad it with empty cells
  // and close the row
  while ($j++ < $max_colunas) {
 echo "    <td></td>'n";
 }
 echo "  </tr>'n";
 }
 // Close the table
 echo "</table>";

我想说的是,这里要做的第一件事是"压平"数组-具有多个维度会使其比需要的复杂得多。因此,与其像这样创建$conteudo

$conteudo = array($online, $ocupado, $offline);

改为:

$conteudo = array_merge($online, $ocupado, $offline);

然后你可以这样做:

$max_colunas = 2; // numero de colunas
// Start the table
echo '<table width="100%" cellpadding="5" border="1">'."'n";
// Loop all the objects
for ($i = 0, $j = 0; isset($conteudo[$i]); $i++) {
  if ($j == 0) {
    // Output the beginning of a row
    echo "  <tr>'n";
  }
  // Always output a data cell
  echo "    <td>$conteudo[$i]</td>'n";
  if (++$j >= $max_colunas) {
    // Output the end of a row and reset the cell counter
    echo "  </tr>'n";
    $j = 0;
  }
}
if ($j) {
  // We may end up with an incomplete row at the end, so pad it with empty cells
  // and close the row
  while ($j++ < $max_colunas) {
    echo "    <td></td>'n";
  }
  echo "  </tr>'n";
}
// Close the table
echo "</table>";

看到它工作

编辑

尝试使用此代码生成数组。请注意,您的输出数组的结构已经更改,以适应我上面的代码示例——如果您在脚本中的其他任何地方使用这些数据,您也需要修改该代码。我对此进行了修改,使得只有一个数据库查询,这似乎就是所需的全部内容。我还修改了它,使$atendentedb保存所有用户数据,包括statusinfo密钥,并且所有行都包含一个horario密钥。

由于您的输入数组最初包含的数据比此代码创建的数组包含的数据要多,因此代码可能需要进一步修改,但请尝试一下,看看进展如何

//group people
$online = $ocupado = $offline = array();
//select people to group
$query = "SELECT nome, status, hp_online, hp_busy, hp_offline, horario
          FROM atendentes
          ORDER BY nome";
$atendentes = mysql_query($query) or die(mysql_error());
$atendentedb = array();
// put selected people in array
while ($row = mysql_fetch_assoc($atendentes)) {
  $atendentedb[strtolower($row['nome'])] = array_merge($row, array('online' => FALSE));
}
//take people online now and check in selected people
$names = modWhosonlineCustom::getOnlineUserNames();
foreach ($names as $name) {
  $uname = strtolower($name->username);
  if (isset($atendentedb[$uname])) $atendentedb[$uname]['online'] = TRUE;
}
//check each selected people
foreach ($atendentedb as $name => $atendente) {
  //if selected people online
  if ($atendente['online']) {
    //verify and 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'];
  }
}

我认为您应该更新内部的计数器CCD_ 10,因此它将对内部阵列的元素进行计数,并将CCD_。if (!is_int($i/2)) { print '<tr><td>' }