Jump to content

Help creating multiplication and addition tables in PHP


phspanthers

Recommended Posts

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>

 

Link to comment
Share on other sites

I was bored, it isn't perfect but it works and needs to be stylized: 

<?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'];
}


?>
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Learning How to Multiply & Add</title>
  <style>
    table {
     border-collapse: collapse;
   }
   table, th, td {
     border: 1px solid #2e2e2e;
   }
 </style>
</head>

<body>
  <form action="" method="post">
    <label for="numRows" class="labelStyle">Number of Rows:</label>
    <input id="numRows" name="rows" value="">
    <label for="numCols" class="labelStyle">Number of Cols:</label>
    <select id="numCols" 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>
    <br>
    <h3>Operation</h3>
    <input id="multiplication" type="radio" name="operation" value="multiplication" checked>
    <label for="multiplication">Multiplication</label>
    <input id="addition" type="radio" name="operation" value="addition">
    <label for="addition">Addition</label>
    <br>
    <input class="submitBtn" type="submit" name="submit" value="Generate">
  </form>
  <table class="tableStyle">
    <thead>
      <tr>
        <!-- Number of Colums and Rows are initial set to 10 -->
        <?php $columns = isset($numCols) ? $numCols : 10; ?>
        <th colspan="<?php echo $columns ?>"><?php echo $columns . " Columns"; ?></th>
      </tr>
      <tr>
        <?php
        for($x = 0; $x < $columns; $x++) {
         echo '<th>' . $x . '</th>' . "\n";
       }
       ?>
     </tr>
   </thead>
   <tbody>
     <?php

     $rows = isset($numRows) ? $numRows : 10;
     for($x = 0; $x < $rows; $x++) {
      echo "<tr>\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 "<td>" . $x . " X " . $y . " = " . ($x * $y) . "</td>\n";
      } elseif ( isset($operand) && ($operand == "addition") ) {
        echo "<td>" . $x . " + " . $y . " = " . ($x + $y) . "</td>\n";
      } else {
        echo "<td>" . $x . " + " . $y . " = " . ($x + $y) . "</td>\n";
      }
    }
    echo "</tr>\n";
  }
  ?>
</tbody>

</table>
</body>
</html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.