将PHP/MySQL数据分为3列 [英] Spliting PHP/MySQL data into 3 columns

查看:60
本文介绍了将PHP/MySQL数据分为3列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用PHP从MySQL返回的数据创建3个HTML列.我希望数据在所有3列之间平均分配...我该怎么做?

I need to create 3 HTML columns in PHP with data returned from MySQL. I would like the data split evenly between all 3 columns... How would I go about doing this?

推荐答案

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

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>';

这篇关于将PHP/MySQL数据分为3列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆