Advertisement
dimipan80

Show Annual Expenses

Apr 22nd, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. <!--Write a PHP script AnnualExpenses.php that receives n years from an input HTML form and creates a table with random expenses by months and the corresponding years (n years back). For example, if N is 10, create a table that shows the expenses for each month for the last 10 years. Add a "Total" column at the end, showing the total expenses for the same year. The random expenses in the table should be in the range [0…999]. Styling the page is optional.-->
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5. <head lang="en">
  6.     <meta charset="UTF-8"/>
  7.     <title>Show Annual Expenses</title>
  8.     <style type="text/css">
  9.         table, th, td {
  10.             border: 1px solid #000;
  11.         }
  12.     </style>
  13. </head>
  14. <body>
  15. <form method="post">
  16.     <p>
  17.         <label for="number-years">Enter number of years:</label>
  18.         <input type="number" name="yearsNum" id="number-years" min="1" max="50" required/>
  19.         <input type="submit" value="Show costs"/>
  20.     </p>
  21. </form>
  22. <table>
  23.     <thead>
  24.     <tr>
  25.         <th>Year</th>
  26.         <th>January</th>
  27.         <th>February</th>
  28.         <th>March</th>
  29.         <th>April</th>
  30.         <th>May</th>
  31.         <th>June</th>
  32.         <th>July</th>
  33.         <th>August</th>
  34.         <th>September</th>
  35.         <th>October</th>
  36.         <th>November</th>
  37.         <th>December</th>
  38.         <th>Total</th>
  39.     </tr>
  40.     </thead>
  41.     <tbody>
  42.     <?php
  43.     if (!array_key_exists('yearsNum', $_POST) || intval($_POST['yearsNum']) <= 0) {
  44.         die('The Form can\'t been Empty!!!');
  45.     }
  46.  
  47.     $yearsNum = (int)$_POST['yearsNum'];
  48.     $currentYear = (int)date("Y", time());
  49.     for ($i = $currentYear - 1; $i >= ($currentYear - $yearsNum); $i--) {
  50.         $total = 0;
  51.         ?>
  52.         <tr>
  53.             <td><?= $i ?></td>
  54.             <?php
  55.             for ($j = 0; $j < 12; $j++) {
  56.                 $month_expenses = rand(0, 999);
  57.                 $total += $month_expenses;
  58.                 echo "<td>$month_expenses</td>";
  59.             }
  60.             ?>
  61.             <td><?= $total ?></td>
  62.         </tr>
  63.     <?php
  64.     }
  65.     ?>
  66.     </tbody>
  67. </table>
  68. </body>
  69. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement