I know I've been here before, but I need additional help. I somehow need the following table to have HTML code that will create a table with two columns 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 ie. 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 ave 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.


<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;


?>

You have "Width" in your form but uses $_POST["width"]

thanks! I fixed that, but it still doesn't work

Member Avatar for diafol
 $rows = $_POST['Width'];  
 $columns = $_POST['height']; 

No space between var name and item name.

The multiplication table formula actually works with the space and without, but I need a seperate form to show up first that you can plug numbers into that will make that table. So the first thing you see when you go to the table.php page is a table with 2 columns and 3 rows. The top left will have either the words Rows or Width in it. The row directly below that will have the words Columns or Height in it. The top right 1st and 2nd rows are for you to put the numbers into for what every you want to multiply, 10 x 10 for example when you click the submit button will produce a 10 x 10 table. The submit or calculate button goes in the 3rd/bottom row. Thanks guys!

I'm losing my mind with this stuff! I still haven't finished my grading one and I have been literally been working almost non stop for several days now!

Member Avatar for diafol
$table = '';
if(isset($_POST['Width']) && isset($_POST['height']))
{
    $rows = (int) $_POST['height'];
    $cols = (int) $_POST['Width'];

    $i=1;
    $table = '<table>';
    for($r=0;$r<$rows;$r++)
    {
        $table .= '<tr>';
        for($c=0;$c<$cols;$c++)
        {
            $table .= '<td>'.$i.'</td>';
            $i++;   
        }
        $table .= '</tr>';   
    }
    $table .= '</table>';
}
?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Create HTML Tables with JS or PHP</title>
<style>
table
{
    border-collapse:collapse;
    border: solid 1px black;
}

table tr td, table tr th
{
    border: solid 1px black;
}
</style>
</head>
<body>

<form method="post">
    <table>
        <tr><th>Height</th><td><input id="height" name="height" value="<?php if(isset($rows)){ echo $rows;}else{ echo 1; } ?>" type="number" min="1" max="50" /></td></tr>
        <tr><th>Width</th><td><input id="Width" name="Width" value="<?php if(isset($cols)) { echo $cols;} else { echo 1; } ?>" type="number" min="1" max="50" /></td></tr>
        <tr><td colspan="2"><input id="sub" value="Create Table" type="submit" /></td></tr>
    </table>
</form>

<div id="newtable">
<?php 
    echo $table;
?>
</div>
</body>
</html>

If you can't get that to work I don't know what's wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.