<?php
  if (isset($_GET['source'])) {
        highlight_file($_SERVER['SCRIPT_FILENAME']);
        exit;
  }
?>
<html>
<head>
<title>PHP Multiplication Table</title>
    
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="theme-color" content="#B5C7DD" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="../css/reset.css">
        <link rel="stylesheet" href="../css/mandy.css">
        <link rel="stylesheet" href="../css/form.css">
        <link rel="stylesheet" href="../css/multiply.css">

<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
</head>
<body>

<h2 class="pad-top">    
    
<?php   # need this to be able to view source in the below anchor tag
  $urlPath=dirname($_SERVER['SCRIPT_URL']).'/'; 
  $labsPath=$urlPath.'labs/'; 
?>     

    <a href="../index.php">CS 130A</a> → Lab 7
<a href="<?=$urlPath?>multiply.php?source" target="labs">Source code</a>

</h2>   


<h4>Multiplication Table</h4>
    <p class="nudge in">Click a button below to generate a square multiplication table of the size indicated as well as a multi-dimensional array of the results.</p>

 <form action="multiply.php#result" method=post name="render" id="render"> 
  
<div class="nudge flex-center wrap-around">
     
<input type="submit" id="1" name="input" value="1" class="space button">
    
<input type="submit" id="2" name="input" value="2" class="space button">
    
<input type="submit" id="3" name="input" value="3" class="space button">
    
<input type="submit" id="4" name="input" value="4" class="space button">
    
<input type="submit" id="5" name="input" value="5" class="space button">
<!--</div>-->
     
<!--<div class="flex-center wrap-around">      -->
<input type="submit" id="6" name="input" value="6" class="space button">
    
<input type="submit" id="7" name="input" value="7" class="space button">
    
<input type="submit" id="8" name="input" value="8" class="space button">
    
<input type="submit" id="9" name="input" value="9" class="space button">
    
<input type="submit" id="10" name="input" value="10" class="space button">
    
</div>     
      
</form>
    
<div id="result"></div>        

<?php 
 //  obtains the value from the names of input from the form from index.php and sets the variable $input to the selected number    
$input = $_POST['input'];
$index = ($input - 1);
   
 // output a numerical array (starting from 1, ending at $input, step size of 1)
$th = range(1, $input, 1);

 // sets the $result array to initually be an array containing the header array $th
 // set $count to count the loops performed to output the final $result array
$result = array($th);
$count = 1;
   
 // create a function called row that: 
    #   sets $count to a global variable so it can be accessed by row
    #   returns the array multiplied by the value contained in $count
function row($a)
    {
    global $count;
    return($a * intval($count));
    }   
    
 // while the $count is less than $input (the current max size of the table): 
    #   incrementally increase $count by 1
    #   add a new value to the $result array, which is an array created by
            #   an array map generated by calling the row function using the variable $th
    #   these actions loop until $count = $input, then it stops and the $result array is complete
    while ($count < $input)
        {$count = ($count + 1);
         array_push($result, array_map("row", $th));
        };

 // the $result array of the products is printed after the table
 // short echo below to control table format using css classes depending on the input
?> 
    
 <table class="pad-top _<?=$input?>" id="table">
     
  <tr>
    <th></th>      
<?php    // defines the count variables to keep track of how many times a line of table is printed
$counT = 0;     # printed lines of column <th>
$counR = 0;     # printed lines of row <th>
$counTD = 0;    # printed lines of row <td>
     
//  as long as the number of column <th> is less than the input value
    while ($counT < $input)   
        {$counT = ($counT + 1);     # increment column count $counT
         print "\t<th scope=\"col\">$counT</th>\n"; # then print the column th with $counT. it's important to increment first or else the prints will start at 0 instead of 1
        };
?>
  </tr>
     
<?php 
//  to start this loop, there are no printed lines of row <th> $counR
//  keep looping as long as lines of row <th> $counR is less than $input
     #  print format <tr><th> row setting the integer %d = ($counR + 1)
     #  enter another forloop 
        //  to start this loop, there are no printed lines of row <td> $counTD
        //  keep looping as long as lines of row <td> $counTD is less than $input
             #  print <td> $result array with [row][column] as indexes as $result[($counR)][($counTD)
        //  increment $counTD
     #  print </tr> closing tag when the inner loops of <td> are done printing
//  keep looping as long as lines of row <th> $counR is less than $input
//  increment $counR
    for ($counR = 0; $counR < $input; $counR++) 
        {
        printf ("\t<tr>\n \t\t<th scope=\"row\">%d</th>\n", $counR + 1);
        for ($counTD = 0; $counTD < $input; $counTD++) 
            {
            print "\t\t\t<td>{$result[intval($counR)][intval($counTD)]}</td>\n";
            };
        print "\t</tr>\n\n";
        };   
?>           
    </table>     

<h4 class="nudge">$result Array</h4>    
    <pre>
<?php  
// the $result array of the products is printed   
print_r ($result); 
?>
    </pre>    
    
<!--
    <p class="hide">
        formatting paragraph
    </p>
-->
    
    <div class="stretch"></div>
</body>
</html>