Php 表+foreach-新列?

Php 表+foreach-新列?,php,html,foreach,html-table,Php,Html,Foreach,Html Table,我怎么做两列 aaa1 aaa2 ... aaa50 ... aaa90 ... aaa100 但没有: aaa1 aaa50 aaa2 .... ... aaa90 aaa50 aaa100 既然您已经对其进行了编号,只需将其提取下来: aaa1 aaa2 aaa3 aaa4 ... .... aaa99 aaa100 完全一样 a1 a51 a2 a52 ... a49 a99 a50 a100 这使您拥有以下代码: a1 a1+50 a2 a2+50 ..

我怎么做两列

aaa1
aaa2
...
aaa50
...
aaa90
...
aaa100
但没有:

aaa1   aaa50
aaa2   ....
...    aaa90
aaa50  aaa100

既然您已经对其进行了编号,只需将其提取下来:

aaa1 aaa2
aaa3 aaa4
...  ....
aaa99 aaa100
完全一样

a1  a51
a2  a52
...
a49 a99
a50 a100
这使您拥有以下代码:

a1  a1+50
a2  a2+50
...
a50 a50+50

注意:这要求您以这种方式生成表。如果它实际上不是那样编号的,那么您必须找到另一种方法来生成清单2中的抽象表,只需考虑条目相对于其左邻居的索引。

如果这对您有效,请尝试。无论$values中包含的项的格式如何,它都应该按您想要的方式打印数组

echo "<table>";

for($i=1; $i<=50; $i++) {
    echo "<tr><td>" . $i . "</td><td>" . ($i+50) . "</td></tr>";
}

echo "</table>";

我这样做的方法是创建两个单独的表,每个表都有一列宽,然后将它们都包含在一个单列、两列的表中:

<?php

$size = count($values);
$halfSize = intval(ceil($size / 2));

for ($i = 0; $i < $halfSize; $i++) 
{
    $j = $i + $halfSize;

    echo "<tr><td>" . $values[$i]  . "</td>";

    // If $size is odd, $halfSize is rounded up. This means that if we have 
    // 3 elements, it will try to access to $values[3], since $halfSize would
    // be 2. The last second column element should be blank in this case.
    // So, if $halfSize is odd, and $i is the last element, don't print an
    // additional table cell.

    if (!($i == $halfSize - 1 && $size % 2 == 1))
        echo "<td>" . $values[$j]  . "</td>";

    echo "</tr>";
} 

?>
添加一些CSS以删除子表和边框等周围的填充,您应该可以很好地使用。

a函数

<?php
$list=array('a','b','c','d','e','f');
$midpoint=floor(count($list)/2);
$tableHeader='<table width="100%">';
$tableFooter='</table>';
$leftTable=$tableHeader;
$rightTable=$tableHeader;
for ($c=0; $c<$midpoint; $c++)
{
    $leftTable.='<tr><td width="100%">'.$list[$c].'</td></tr>';
}
$leftTable.=$tableFooter;
for ($c=$midpoint; $c<count($list); $c++)
{
    $rightTable.='<tr><td width="100%">'.$list[$c].'</td></tr>';
}
$rightTable.=$tableFooter;
$mainTable='<table><tr><td width="50%">'.$leftTable.'</td><td width="50%">'.$rightTable.'</td></tr></table>';
echo $mainTable;
?>
密码

function sqlArr($sql){
  $ret = array();
  $res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
  if ($res) {
    while($row = mysql_fetch_array($res)){
      $ret[] = $row;
    }
  }
  return $ret;
}
模板

$temp = sqlArr("SELECT value FROM table");
$data = array();
for($i=0;$i<50;$i++){
  $data[] = array($temp[$i],$temp[$i+50]);
}
unset($temp);

很好的评论,这也很大程度上取决于他以后打算对表数据做什么。你必须坚持使用标记吗?有一个很好的解决方案,使用不同的标记结构。我将为数据库获取这些值,这不是NumberTanks,但这为我生成了第一列:1-100应该是1-50,在第二列50-100中,这是确定的
$temp = sqlArr("SELECT value FROM table");
$data = array();
for($i=0;$i<50;$i++){
  $data[] = array($temp[$i],$temp[$i+50]);
}
unset($temp);
<table border='1'>
<? foreach ($data as $i => $row): ?>
  <tr>
  <? foreach ($row as $cell): ?>
    <td><?=$cell?></td>
  <? endforeach ?>
  </tr>
<? endforeach ?>
</table>