Jump to content

phspanthers

New Members
  • Posts

    1
  • Joined

  • Last visited

phspanthers's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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. <html> <head/> <body> <form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>"> <table border="1"> <tr><td>Number of Rows:</td><td><input type="text" name="rows" /></td></tr> <tr><td>Number of Columns:</td><td><select name="columns"> <option value="1">1</option> <option value="2">2</option> <option value="4">4</option> <option value="8">8</option> <option value="16">16</option> </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>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.