接受来自用户的输入并显示数组和乘法(CLOSE)

时间:2015-11-03 09:33:41

标签: php multidimensional-array

这是来自用户和显示数组和乘法的接受输入的代码,并以表格形式表示。

       <?php

        if(isset($_POST['submit']))
        {
            $n1 = $_POST['num1'];
            $n2 = $_POST['num2'];
        }
        else 
        {
            $n1 = 0;
            $n2 = 0;
        }


        function UniqueRandomNumbersWithinRange($min, $max, $quantity) {
            $numbers = range($min, $max);
            shuffle($numbers);
            return array_slice($numbers, 0, $quantity);
        }



         $numberArray = array(
            UniqueRandomNumbersWithinRange(1,10,$n1),
            UniqueRandomNumbersWithinRange(1,10,$n2))
        ?>            
     <form action="" method="post">

         <input type="text" name="num1" placeholder="value 1"><br>
         <input type="text" name="num2" placeholder="value 2"><br>
        <input type="submit" name="submit">
     </form>    
          <?php

        printTable($numberArray); //panggilan function apa yg nk tunjuk
        function printTable($numberArray) {
            // Placeholder
            $result = [];

            // Setup the multiply
            foreach ($numberArray[1] as $key1 => $value1) {
                $tmp = array($value1); // add index y-axis
                foreach ($numberArray[0] as $key0 => $value0) {
                    $tmp[] = $value0 * $value1;
                }
                $result[] = $tmp;
            }

            // Add index the x-axis
            array_unshift($result, array_merge(array(" "), $numberArray[0]));

            // display the result into table form
            echo "<table border='1' align='center'>";
            foreach ($result as $key => $value) {
                echo "<tr>";
                foreach ($value as $k => $v) {
                    if ($k == 0 || $key == 0) {
                        echo sprintf("<td><b>%s</b></td>", $v);
                        continue;
                    }
                    echo "<td>$v</td>";
                }
                echo "</tr>";
            }
            echo "</table>";
        }

        ?>
  1. 这里可以接受来自用户的插入并以数组形式自动生成数字,这意味着用户在textbox1中输入5,在textbox2中输入6,它将生成数字3,4,5,2,1和2,3,4, 1,2,3。
  2. 如何捕获这个数字

    $ numberArray = array(                 阵列($ display1),                 阵列($显示2)             );

0 个答案:

没有答案