Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. Task:
  2. Produce a web page that has two Multiplication tables in it Each table is to be produced by one loop and you will need
  3. to demonstrate a FOR loop and a WHILE loop to complete this task, hence, creating two separate tables.
  4. Page Requirements Alternate rows shall be coloured using html attribute names Every cell containing the result of the square
  5. of a number (1x1, 2x2, 3x3 etc) shall also have distinctive background using a html attribute name Create your times table from 1 to12.
  6. (ie: 1x1 ... 12x12) - for both times tables. Please display only the result (i.e: 1, 2, 4, 144) for the FOR loop table and
  7. display the calculation and result (i.e: 1x1=1, 2x2=4, etc) for the WHILE loop table.
  8.  
  9. I have broken down the completion of this task into 3 different versions.
  10. These versions document the coding process: from table design, to operation through loops, to the final update of the table design.
  11.  
  12. <!DOCTYPE html>
  13. <html lang="en">
  14. <head>
  15. <meta charset="utf-8">
  16. <title>Times tables to 12</title>
  17. </head>
  18. <body>
  19. <h1>Multiplication tables with PHP</h1>
  20. <h2>Multiplication table using a while loop. </h2>
  21. <table width="100%" border="1" align="center">
  22. <caption fontsize="14">Multiplication tables of 1 to 12 with a "while loop".</caption>
  23. <tr style="background-color:lightblue">
  24. <th>Timetable of 1</th>
  25. <th>Timetable of 2</th>
  26. <th>Timetable of 3</th>
  27. <th>Timetable of 4</th>
  28. <th>Timetable of 5</th>
  29. <th>Timetable of 6</th>
  30. <th>Timetable of 7</th>
  31. <th>Timetable of 8</th>
  32. <th>Timetable of 9</th>
  33. <th>Timetable of 10</th>
  34. <th>Timetable of 11</th>
  35. <th>Timetable of 12</th>
  36. </tr>
  37. <?php
  38. #Declare a function with 2 arguments
  39. function times ($x, $y) {
  40. #Inside the function, multiply the 2 numbers
  41. $tables = $x * $y;
  42. #and return the result
  43. return $tables;
  44. }
  45.  
  46. #In the main PHP script, create a variable for the first number
  47. $x = 1;
  48. $tables = "";
  49. #While the first number is less than 13, repeat the following loop
  50. while ($x < 13) {
  51. #Create another variable for the second number
  52. $y = 1;
  53. #Set the background for each alternating row
  54. $bg_color = $x % 2 === 0 ? "lightgrey" : "lightpink";
  55. #Open a table row
  56. echo "<tr style='background-color: ". $bg_color .";'>";
  57. #While the second number is less than 13, repeat the following loop
  58. while ($y < 13) {
  59. #Call the function
  60. $tables = times ($x, $y);
  61. #Test values for square numbers and print special background
  62. if ($tables ? $x%$y == 0 && $x==$y: $x%$y !== 0) {
  63. $bg_color = "yellow";
  64. echo "<td style='background-color: ". $bg_color .";'><b>$x X $y is:$tables</b></td>";
  65. }else {
  66. #Return remaining numbers
  67. echo "<td> $x X $y is: $tables </td>";}
  68. #Increment the value of the second variable
  69. $y++;
  70. }
  71. #Increment the value of the first variable
  72. $x++;
  73. #Close the table row
  74. echo "</tr>";
  75. }
  76. ?>
  77. </table>
  78.  
  79. <h2>Multiplication table using a for loop. </h2>
  80. <table width="100%" border="1" align="center">
  81. <caption>Multiplication tables of 1 to 12 with a "for loop".</caption>
  82. <tr style="background-color:lightblue">
  83. <th>Timetable of 1</th>
  84. <th>Timetable of 2</th>
  85. <th>Timetable of 3</th>
  86. <th>Timetable of 4</th>
  87. <th>Timetable of 5</th>
  88. <th>Timetable of 6</th>
  89. <th>Timetable of 7</th>
  90. <th>Timetable of 8</th>
  91. <th>Timetable of 9</th>
  92. <th>Timetable of 10</th>
  93. <th>Timetable of 11</th>
  94. <th>Timetable of 12</th>
  95. </tr>
  96. <?php
  97. $result = "";
  98. #For each value of i below 13, repeat the following loop
  99. for ($i = 1; $i < 13 ; $i++) {
  100. #Set the bakcground for each alternating row
  101. $bg_color = $i % 2 === 0 ? "lightgrey" : "lightpink";
  102. #Open a table row
  103. echo "<tr style='background-color: ". $bg_color .";'>";
  104. #For each value of n up to 13, repeat the operation and print the results
  105. for ($n = 1; $n < 13 ; $n++) {
  106. $result = $i * $n;
  107. #Test results for square numbers and print special background
  108. if ($result ? $i%$n == 0 && $i==$n: $i%$n !== 0) {
  109. $bg_color = "yellow";
  110. echo "<td style='background-color: ". $bg_color .";'><b>$result</b></td>";
  111. }else {
  112. #Return remaining numbers
  113. echo "<td> $result </td>";}
  114. }
  115. #Close the table row
  116. echo "</tr>";
  117. }
  118. ?>
  119. </table>
  120. </body>
  121. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement