Reputation: 11
I'm very new to this so bear with me.
I somehow need the following table to have HTML code that will create a table with two column and three rows. The top left column will have the title rows, the middle will have columns, the bottom will have a submit button so that when you add numbers to the right columns i.e. put 10 in the row text box and 10 in the column text box and a 10 X 10 multiplication table will be created.
I have the following PHP and HTML codes, but they are not working together. Please Help!
I thought that I could get the HTML code to work in the PHP document.
The html code is supposed to have two text boxes, one for rows and one for columns that when you add a number to a row and a column, it will create a multiplication table that big.
<form name="table" id="table" action="table.php" method="post">
<h2>Table Generator</h2>
<p>
<label for="width">Rows:</label>
<input type="text" name="Width" id="Width" />
</p>
<p>
<label for="height">Columns:</label>
<input type="text" name="height" id="height" />
</p>
<input name="sbt" type="submit" formaction="table.php" onClick="MM_validateForm ('width','','RisNum','height','','RisNum');return document.MM_returnValue" value="Calculate" />
</form>
<?php
$rows = $_POST ['width'];
$columns = $_POST ['height'];
$width = 5;
$height = 6;
$i=1;
$table='<table border="1">';
for($r=0;$r<$width;$r++)
{
$table .= '<tr>';
for($c=0;$c<$height;$c++)
{
$table .= "<td>$i</td>";
$i++;
}
$table .= '<tr>';
}
$table .= '</table>';
echo $table;
?>
Upvotes: 1
Views: 12470
Reputation: 12641
Your for loop should look something more like this:
for($r=0; $r < $height; $r++)
{
$table .= '<tr>';
for($c=0; $c < $width; $c++)
{
$table .= "<td>" . ($r * $c) . "</td>";
}
$table .= '</tr>';
}
For a start, you should be looping through the height first (while you create the rows), then the width (for the columns). Secondly, you weren't multiplying the two values, you were only outputting the width loop veriable, so you should be outputting the result of multiplying both ($r * $c
).
Also, you don't need another variable in the loop (you had $i).
Lastly, you weren't closing the tag, you were opening a new one at the end of the loop.
Upvotes: 1