How do I use .php to generate a square or rectangle that is created using a letter or number based on how many rows/columns a user enters? Here is a sample of what I am looking for. I've done this with javascript but wanted to learn some server side scripting (.php). Started a couple weeks ago so forgive my novice errors.
FFFFF
FFFFF
FFFFF
FFFFF
FFFFF
Here is what I have so far:
<?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>
********************************************
This is how I solved this a different way using js.
function forTangle() {
var num_rows = document.getElementById('rows').value;
var num_cols = document.getElementById('cols').value;
var theader = '<table border="0">
';
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>
';
}
var tfooter = '</table>';
document.getElementById("output3").innerHTML ="Nested for loop rectangle" + "<br>" + theader + tbody + tfooter; <!--output to screen-->
}
erm im not too sure what this will be used for.... but there you.
$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>';
Happy coding. Welcome to the PHP community :)