?> Multiplication Table

Generate a Multiplication Table


<?php
include ('../header/assignmentHeader.php');


    
// Check if the form has been submitted via POST
    
if ($_SERVER["REQUEST_METHOD"] == "POST") {

        
// Grab the user input and convert it to an integer for sanitization/validation.
        
$tableSize = (int)$_POST['size'];

        
// Add a basic safety check so users cannot enter large numbers and break the page
        
if ($tableSize >= 1 && $tableSize <= 20) {

            echo 
"<h3>Showing a {$tableSize}x{$gridSize} Table:</h3>";
            echo 
"<table border='1' cellpadding='5' cellspacing='0'>"; // styling of grid/table.

            // Outer for loop: Handles the rows - increments.
            
for ($row = 1; $row <= $tableSize; $row++) {
                echo 
"<tr>";

                
// Inner for loop: Handles the columns based on 'size' above.
                
for ($col = 1; $col <= $tableSize; $col++) {
                    
$product = $row * $col;
                    echo 
"<td>$product</td>";
                }

                echo 
"</tr>";
            }

            echo 
"</table>";

        } else {
            echo 
"<p style='color:red;'>Please enter a number between 1 and 20.</p>";
        }
    }
    
?>
?>


    <!DOCTYPE html>
    <html>
    <head>
        <title>Multiplication Table</title>
        <link rel="stylesheet" href="../css/assignmentHeaderStyles.css" />
    </head>
<body>

<h2>Generate a Multiplication Table</h2>

<!--//Submits it to the page -->
<form method="POST" action="">
    <label for="size">Enter grid size (1-20):</label>

    <input type="number" id="size" name="size" min="1" max="20" required value="
    <?php echo isset($_POST['size']) ? htmlspecialchars($_POST['size']) : 10; ?>">
    <button type="submit">Create your grid</button>
</form>

<br>
</body>
</html>

<?php
show_source
(__file__);
include(
'../footer/footer.php');
?>