php 动态生成_用PHP动态生成表

php 动态生成

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths in the for loop.

动态生成表是php开发人员面临的最常见问题...。因此似乎需要一篇文章来解释动态生成表的基本概念。 它只需要html的基本知识和for循环中的少量数学运算即可。

Formatting data and displaying it in a grid format is needed most of the times. PHP developers who are good at coding also face this kind of problem due to lack of html knowledge. Also I have seen many questions at experts exchange who asks for formatting data and dynamically creating table. Most of the times developer stuck at the basic row/column problem.

大多数时候需要格式化数据并以网格格式显示。 由于缺乏html知识,擅长编码PHP开发人员也面临此类问题。 在专家交流中,我还看到了许多问题,谁要求格式化数据和动态创建表。 大多数时候,开发人员都停留在基本的行/列问题上。

Let me explain you this concept with example. So lets consider I have to generate a table where each row(horizontal) consists of 3 columns (vertical). Basic html syntax used will be <table> , <tr>  (row) , <td> (column).

让我通过示例向您解释这个概念。 因此,考虑一下我必须生成一个表,其中每行(水平)由3列(垂直)组成。 使用的基本html语法将为<table>,<tr>(行),<td>(列)。

Basic scenario is as follows:

基本方案如下:

Input:

输入:

$arr = array('a','b','c','d','e','f','g','h','i'); // test array containing 9 elements to be displayed in grid format

$ arr = array('a','b','c','d','e', 'f','g','h ','一世'); //测试包含9个要以网格格式显示的元素的数组

Expected output:

预期产量:

--------------

--------------

  a  |  b |  c |

一个| b | c |

--------------

--------------

  d  |  e |  f  |

d | e | f |

--------------

--------------

  g  |  h |  i  |

g | h | 我

--------------

--------------

So here is the code to dynamically generate table in php :

所以这是在php中动态生成表的代码:

<?php

echo "Creating table dynamically";

$arr = array('a','b','c','d','e','f','g','h','i'); // test array containing 9 elements to be displayed in grid format

$tot_cnt = count($arr);                        // calculating the count of elements in the array i.e. 9

echo "<table border=1><tr>"; // start table and open first row

for( $i = 0 ; $i < $tot_cnt; $i++){

  // In the below if loop a check is performed to see that if 3 columns 
 // are created than close the old row and open new row

   if($i % 3 == 0 && $i  !=0 ){ 
      echo "</tr><tr>";                // closing old row and opening new row 
   }			 

  echo "<td>".$arr[$i]."</td>"; // add data in columns

}

echo "</tr></table>"; // close last row and table
?>

you can see in the above code that for placing 3 columns in a row this condition is important:

您可以在上面的代码中看到对于将3列连续放置此条件很重要:

   if($i % 3 == 0 && $i  !=0 ){ 
      echo "</tr><tr>";                // closing old row and opening new row 
   }			 

so if you need to place say 5 columns in a row than do just this.....

因此,如果您需要连续说5列,那么您需要这样做。

   if($i % 5 == 0 && $i  !=0 ){ 
      echo "</tr><tr>";                // closing old row and opening new row 
   }			 

So basically the number with which you perform mod(%) operation in crucial. The math here is simple ... the mod(%) operator gives the remainder of the division so say for example 25 % 3 will be 1. hope you are clear with the mod concept if not I have provided a reference urls below you may checkout those.

因此,基本上,执行mod(%)操作所用的数量至关重要。 这里的数学很简单... mod(%)运算符给出除法的余数,例如25%3将是1。希望您对mod概念很清楚,如果没有,我可能会在下面提供参考网址结帐那些。

Hope this helps to most of the php developers searching for formatting data in a table.

希望这有助于大多数php开发人员在表中搜索格式数据。

References for your help:

为您提供帮助的参考:

If you want to understand how html table works check out this ...

如果您想了解html表的工作原理,请查看此...

http://www.w3schools.com/html/html_tables.asp http://www.w3schools.com/html/html_tables.asp

if you want to understand how moduli operator works check out this:

如果您想了解模运算符的工作原理,请查看以下内容:

http://php.net/manual/en/language.operators.arithmetic.php http://php.net/manual/zh/language.operators.arithmetic.php

-Sandeep

-山迪普

翻译自: https://www.experts-exchange.com/articles/6970/Dynamically-generating-tables-in-PHP.html

php 动态生成

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值