且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将PHP/MySQL数据分为3列

更新时间:2022-03-16 00:06:04

您可以尝试执行以下操作:

You could try doing something like this:

$result = mysql_query("SELECT value FROM table");
$i = 0;
echo '<table><tr>';
while ($row = mysql_fetch_row($result)){
  echo '<td>' . $row[0] . '</td>';
  if ($i++ == 2) echo '</tr><tr>'
}
echo '</tr></table>';

请注意,此表的值按如下顺序排列

note this table has the values ordered like

1 2 3 
4 5 6
7 8 9

如果您想像垂直方向那样

If you wanted it vertically like

1 4 7
2 5 8
3 6 9

然后您应该做类似

$result = mysql_query("SELECT value FROM table");
$data = Array();

while ($row = mysql_fetch_row($result)) $data[] = $row;

for ($i = 0; $i < count($data) / 3; $i++){

  echo '<table><tr>';

  for ($j = 0; $j < 3; $j++){
    echo '<td>' . $data[ $i + $j * 3] . '</td>';
  }

  echo '</tr><tr>'
}
echo '</tr></table>';