如何从用户输入中生成一个输出字母或数字的正方形


How can i generate a square from user input that outputs letters or numbers

如何使用.php生成一个正方形或矩形,该正方形或矩形是根据用户输入的行/列数使用字母或数字创建的?这是我要找的一个样品。我已经用javascript完成了这项工作,但想学习一些服务器端脚本(.php)。几周前开始,所以请原谅我的新手错误。

FFFFF
FFFFF
FFFFF
FFFFF
FFFFF

以下是我目前所拥有的:

<?php
#initial page load
#create functions and call them in program
#create variables
#get user input
$row = filter_input(INPUT_GET, "row", FILTER_VALIDATE_INT);
$col = filter_input(INPUT_GET, "col", FILTER_VALIDATE_INT);
#for loop to generate rectangle or square of "F" or "5"
function forTangle() {
    $theader = '<table border = "0">';
    $tbody = ' ';
    $sum = 0;
    $tfooter = ' ';
    $output = "Nested For Loop";
    for ($i = 0; $i < $row; $i++) {
        $tbody += '<tr>';
        for($j = 1; $j < $col; $j++) {
            $sum = $i + $j;
            $tbody += '<td>';
            $tbody += "$i + $j + = $sum";
            $tbody += '</td>';
        }
        $tbody += '</tr>'; 
    }
        $tfooter = '</table>';
        echo $output;
        #Variable that outputs the table of "F" or "5"???
        #how do i call the DOM and put this in a container? 
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="q3.css">
<title>Allen Ricardo's Magic Rectangle</title>
</head>
<body>
<div class="header">
<header><h1>Rectangle</h1></header>
</div>
<div class="prompt">
<h3>Rectangle</h3>
<br>
<h3><?php "rectangle has ${row}s, and ${col}s."; ?></h3>
</div>
<div class="output">
<p><?php echo forTangle(); ?></p> 
<!--container for rectangles-->

</div>
<div class="link">
<a href="q3_index.html">Back to Homepage</a>
</div>
</body>
</html>
********************************************

这就是我使用js以不同的方式解决这个问题的方法。

function forTangle() {
var num_rows = document.getElementById('rows').value;
var num_cols = document.getElementById('cols').value;
var theader = '<table border="0">'n';
var tbody = '';
var sum = 0;
var i;
var j;
    for(i=1; i<=num_rows;i++)
        {   
            tbody += '<tr>';
            for(j=1; j<=num_cols;j++)
        {
                sum = i + j; <!--sum of row + column number-->
                tbody += '<td>';
                tbody += (i + "+" + j + "=" + sum); <!--values in the table cell-->
                tbody += '</td>';
        }
                tbody += '</tr>'n';
    }
            var tfooter = '</table>';
            document.getElementById("output3").innerHTML ="Nested for loop rectangle" + "<br>" + theader + tbody + tfooter; <!--output to screen-->
}

好了:

$letter = 'F';
$width = 5;
$height = 8;
$row = str_repeat ( '<td>' . $letter . '</td>' , $width );
$rows = [];
for ($i=0; $i < $height; $i++) { 
    $rows[] = '<tr>' . $row . '</tr>';
}
echo '<table>' . implode('', $rows) . '</table>';