在PHP和HTML的帮助下动态创建行和列 [英] Dynamic create rows and colum with the help of PHP and HTML

查看:109
本文介绍了在PHP和HTML的帮助下动态创建行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在PHP和HTML的帮助下创建动态的行和列,但是我对这段代码有些困惑,所以有些帮助是绝对值得赞赏的。

 < table> 
<?php
$ tr = 0;
foreach($ data as $ db_data){
$ tr ++;
if($ tr == 1){
echo< tr>;
}

echo< td>;
echo $ db_data ['id'];
回显< / td>;
}

if($ tr == 2){

}
?>
< / table>

场景很简单:

数据返回6每个循环的记录没有结果将显示像这个图像


同样的方式Mysql数据返回3没有记录的结果会像这个图像一样显示




可能是这样的,也许是

函数create_table()


$ b $ b pre $ 函数create_table($ data){
$ res ='< table width =200border =1>';
$ max_data = sizeof($ data);
$ ctr = 1;
foreach($ data as $ db_data){
if($ ctr%2 == 0)$ res。='< td align =center>'。 $ DB_DATA [身份证]。 < / TD>< / TR>;
else {
if($ ctr< $ max_data)$ res。='< tr>< td align =center>'。 $ DB_DATA [身份证]。 < / TD>;
else $ res。='< tr>< td colspan =2align =center>'。 $ DB_DATA [身份证]。 < / TD>< / TR>;
}
$ ctr ++;
}
返回$ res。 < /表>;

当然,您可以修改表格的样式以适合您的需要。



像这样调用它:

  echo create_table($ data); 



输出



4,3和8 id 的)


如果您传递偶数个id或者表中最后一行被合并的数据,则返回每个列中具有相同行数的表。 / p>

I want to create dynamic rows and column with the help of PHP and HTML but I am little confused about this code so some help is definitely appreciated.

<table>
<?php
  $tr = 0;
  foreach ($data as $db_data) {
    $tr++;
    if ($tr == 1) {
      echo "<tr>";
      }

    echo "<td>";
    echo $db_data['id'];
    echo "</td>";
    }

  if($tr == 2){

    }
?>
</table>

Scenario is so simple:

Mysql data return 6 no of records from for-each loop the result will be show like this image

Same way the Mysql data return 3 no of records the result will be show like this image

解决方案

Something like this, maybe

function create_table()

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.

Call it like this:

echo create_table($data);

Output

(example for 7, 4, 3 and 8 id's)

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.

这篇关于在PHP和HTML的帮助下动态创建行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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