douxian4323
douxian4323
2015-05-05 16:33

PHP使用while和嵌套for循环错误地循环输出

  • php

Attempting to generate table columns and rows based on input. With smaller numbers it appears to work appropriately, for example inputting 2 rows and 3 cols. But when inputting slightly larger numbers like 5 for rows will output an incorrect amount of rows sometimes looping indefinitely.

if(isset($_POST['submit'])) {

    $rows = (int)$_POST['row_num'];
    $cols = (int)$_POST['col_num'];
    $n = 1;
    $e = 0;

    echo '<table id="">';

    while(($e < $rows) && ($e < $cols)) {

        for($i = 0; $i < $rows; $i++) {
            echo '<tr>';

            for($i = 0; $i < $cols; $i++) {
                echo '<td><input type="text" name="field_' . $n . '"></td>';
                $n++;
            }

            echo '</tr>';
        }

        $e++;
    }

    echo '</table>';
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换