且构网

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

借助 PHP 和 HTML 动态创建行和列

更新时间:2023-11-17 14:11:10

可能是这样的

function create_table($data) {
  $res = '<table width="200" border="1">';
  $max_data = sizeof($data);
  $ctr = 1;
  foreach ($data as $db_data) {
    if ($ctr % 2 == 0) $res .= '<td align="center">' . $db_data['id']. '</td></tr>';
    else {
      if ($ctr < $max_data) $res .= '<tr><td align="center">' . $db_data['id']. '</td>';
      else $res .= '<tr><td colspan="2" align="center">' . $db_data['id']. '</td></tr>';
      }
    $ctr++;
    }
  return $res . '</table>';
  }

当然,您可以根据需要修改表格样式.

Course, you can modify style of table to fit your needs.

这样称呼它:

echo create_table($data);

输出

(7、4、3 和 8 id 的示例)

如果传递偶数个 id,则返回每列中行数相同的表,或者如果将奇数个 id 传递给函数,则返回最后一行合并的表.

It returns table with same number of rowsin each column if you pass even number of id's or table where last row is merged if you pass odd number of id's into function.