creating multiplication and addition tables

I am new to php and need help creating addition and multiplication tables using the html code we were given. I feel like I am on the right track, but I am getting a few errors and I cant figure them out. One of the errors is that it tells me I am not putting in a valid number for rows even though it is a positive number so it should work. Here is my code and all help is appreciated thanks in advance.

[php]





Number of Rows:
Number of Columns:
1
2
4
8
16
  </select>
</td></tr>
<tr><td>Operation:</td><td><input type="radio" name="operation" value="multiplication"     checked="yes">Multiplication</input><br/>
<input type="radio" name="operation" value="addition">Addition</input>
</td></tr>
</tr><td colspan="2" align="center"><input type="submit" name="submit" value="Generate" /></td>    </tr>
</table>
</form>


<?php


    //check to see if num of rows is numberic
    if (isset($_POST["rows"]) && is_numeric($_POST["rows"])){
            //check to see if rows is a positive number
            if($_POST["rows"] > 0){


            if(isset($_POST) && $_POST['operation'] == "multiplication")
            {
                    for($r = 0; $r < $_POST["rows"]; $r++){
                            echo '<tr>';


                            for($c = 0; $c < $_POST["columns"]; $c++){
                                    echo '<td>' .$c*$r. '</td>';
                            echo '</tr>';


                            }}
            }


            else if (isset($_POST) && $_POST['operation'] == "addition")
            {
                    for($r = 0; $r < $_POST["rows"]; $r++){
                            echo '<tr>';


                            for($c = 0; $c < $_POST["columns"]; $c++){
                                    echo '<td>' .$c+$r. '</td>';
                            echo '</tr>';
                    }
                 }
            }


            }
            else{
            echo 'Invalid rows columns parameters';
            exit();
            }
    }
    else{
    echo 'Invalid rows columns parameters';
    exit();
    }


?>
</body>
</html>

[/php]

I answered you on a different forum, but I spruced it up a little. (I was still bored)
[php]<?php

/* I would pull the form and table apart, by that I mean /
/
don’t try to do two things at once. I haven’t done /
/
any number validation and I’m sure this needs to be /
/
modified for I don’t know what exactly the initial /
/
HTML looked liked. */

if ( isset($_POST[‘submit’]) && $_POST[‘submit’] == “Generate” ) {
$numRows = $_POST[‘rows’];
$numCols = $_POST[‘columns’];
$operand = $_POST[‘operation’];
}

?>

Learning How to Multiply & Add table { border-collapse: collapse; } table, th, td { border: 1px solid #2e2e2e; padding: 5px; } <?php $columns = isset($numCols) ? $numCols : 10; ?> <?php for($x = 0; $x < $columns; $x++) { echo '' . "\n"; } ?> <?php $rows = isset($numRows) ? $numRows : 10; for($x = 0; $x < $rows; $x++) { echo "\n"; for($y = 0; $y < $columns; $y++) { /* If operand isn't set yet, I set it to an addition default */ if ( isset($operand) && ($operand == "multiplication")) { echo "\n"; } elseif ( isset($operand) && ($operand == "addition") ) { echo "\n"; } else { echo "\n"; } } echo "\n"; } ?>
<?php echo $columns . " Columns"; ?>
' . $x . '
" . $x . " X " . $y . " = " . ($x * $y) . "" . $x . " + " . $y . " = " . ($x + $y) . "" . $x . " + " . $y . " = " . ($x + $y) . "


Number of Rows: Number of Cols: 1 2 4 8 10 16

Operation

Multiplication Addition
[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service