动态表号php

时间:2017-12-23 09:15:15

标签: php for-loop if-statement

我仍然不知道如何使用PHP创建动态表。

我想显示一个包含可变数量的列和行的表,其中包含1到200之间的数字。

Example of the expected output

任何人都可以帮我吗?

2 个答案:

答案 0 :(得分:0)

<?php

for($i=1;$i<=30;$i++) {

  echo "*$i*";
  if($i%10==0){
    echo "<br>";
  }
}

LIVE DEMO

答案 1 :(得分:0)

外部循环中的嵌套循环应该这样做 - 控制行的外部循环和控制列的内部循环。

$rows=20;
$cols=10;
$x=1;

$html=array();
$html[]="<table>";
for( $i=0; $i < $rows; $i++ ){

    $html[]="<tr>";
    for( $j=0; $j < $cols; $j++ ){
        $html[]="<td data-cell='r{$i}c{$j}'>$x</td>";
        $x++;
    }
    $html[]="</tr>";
}

$html[]="</table>";


echo implode( PHP_EOL, $html );

在达到预定义限制时停止创建

$rows=100;
$cols=10;
$x=1;
$limit=200;



$html=array();
$html[]="<table>";
for( $i=0; $i < $rows; $i++ ){

    $html[]="<tr>";
    for( $j=0; $j < $cols; $j++ ){
        $html[]="<td data-cell='r{$i}c{$j}'>$x</td>";
        $x++;
        if( $x > $limit ) break;
    }
    $html[]="</tr>";
    if( $x > $limit ) break;
}

$html[]="</table>";


echo implode( PHP_EOL, $html );