Advertisement
Venciity

Build Table

Aug 26th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Build Table</title>
  6. </head>
  7. <body>
  8.     <form action="" method="post">
  9.         <input type="number" name="start"/>
  10.         <input type="number" name="end"/>
  11.         <input type="submit"/>
  12.     </form>
  13.     <?php
  14.         if (isset($_POST['start']) && isset($_POST['end'])) {
  15.             $start = (int) $_POST['start'];
  16.             $end = (int) $_POST['end'];
  17.  
  18.             function createFibNumbers($maxNum) {
  19.                 $fibNumbers = [1];
  20.                 $f1 = 1;
  21.                 $f2 = 1;
  22.                 while (true) {
  23.                     $f3 = $f1 + $f2;
  24.                     if ($f3 > $maxNum) {
  25.                         return $fibNumbers;
  26.                     }
  27.                     array_push($fibNumbers,$f3);
  28.                     $f1 = $f2;
  29.                     $f2 = $f3;
  30.                 }
  31.             }
  32.  
  33.             $allFibNumbers = createFibNumbers($end);
  34.  
  35.             echo htmlentities("<table>");
  36.             echo "<br>";
  37.             echo htmlentities("<tr><th>Num</th><th>Square</th><th>Fib</th></tr>");
  38.             echo "<br>";
  39.             for ($i = $start; $i <= $end; $i++) {
  40.                 $sqrt = $i*$i;
  41.                 $isFib;
  42.                 if (in_array($i , $allFibNumbers)== 1) {
  43.                     $isFib = "Yes";
  44.                 }
  45.                 else {
  46.                     $isFib = "No";
  47.                 }
  48.                 echo htmlentities("<tr><td>$i</td><td>$sqrt</td><td>$isFib</td></tr>");
  49.                 echo "<br>";
  50.             }
  51.             echo htmlentities("</table>");
  52.         }
  53.     ?>
  54. </body>
  55. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement