MilaDimitrovaa

Homework - Table

Nov 2nd, 2021
958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.20 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Table</title>
  8.  
  9.     <style>
  10.         table, th, td {
  11.             border: 1px solid red;
  12.         }
  13.         th{
  14.             color: #943126;
  15.         }  
  16.         h1{
  17.             color: darkblue;
  18.         }
  19.  
  20.     </style>
  21. </head>
  22. <body>
  23.     <h1>Homework - File Information</h1>
  24.  
  25.    
  26.     <table>
  27.         <thead>
  28.             <tr>
  29.                 <th>Name</th>
  30.                 <th>Salary</th>
  31.                 <th>Town</th>
  32.                 <th>Age</th>
  33.             </tr>
  34.         </thead>
  35.         <tbody>
  36.             <?php  
  37.                 $myfile = fopen("namesInfo.txt", "r") or die("Unable to open file!");
  38.  
  39.                 $all_names = array();
  40.                 $all_salaries = array();
  41.                 $all_towns = [];
  42.                 $all_ages = array();
  43.  
  44.                 $sum = 0;
  45.                 $sumAge = 0;
  46.  
  47.                 while(!feof($myfile)) {
  48.                     $data = fgets($myfile);
  49.  
  50.                     $splitElement = explode(" ", $data);
  51.                     $name = explode(":", $splitElement[0]);
  52.                     $salary = explode(":", $splitElement[1]);
  53.                     $town = explode(":", $splitElement[2]);
  54.                     $age = explode(":", $splitElement[3]);
  55.  
  56.                     array_push($all_names,$name[1]);
  57.                     array_push($all_salaries,$salary[1]);
  58.                     array_push($all_towns,$town[1]);
  59.                     array_push($all_ages,$age[1]);
  60.  
  61.                     $sum += $salary[1];
  62.                     $sumAge += $age[1];
  63.  
  64.                     echo "<tr>";
  65.                         echo "<td>".$name[1]."</td>";
  66.                         echo "<td>".$salary[1]."</td>";
  67.                         echo "<td>".$town[1]."</td>";
  68.                         echo "<td>".$age[1]."</td>";
  69.                     echo "</tr>";
  70.                 }
  71.                 fclose($myfile);
  72.             ?>
  73.         </tbody>
  74.         <tfoot>
  75.             <?php
  76.                 $average = $sumAge/count($all_ages);
  77.                 $count = 0;
  78.                 $currentCount = 0;
  79.                 $currTown;
  80.  
  81.                 for ($i=0; $i < count($all_towns); $i++) {
  82.                     $str = $all_towns[$i];
  83.                     $currentCount = 0;
  84.  
  85.                     for ($j=0; $j < count($all_towns); $j++) {
  86.                         if(strcmp($all_towns[$j],$str) == 0){
  87.                             $currentCount++;
  88.                         }
  89.                     }
  90.  
  91.                     if($currentCount > $count){
  92.                         $count = $currentCount;
  93.                         $currTown = $str;
  94.                     }
  95.                 }
  96.  
  97.                 echo "<tr>";
  98.                     echo "<td>Names : ".count($all_names)."</td>";
  99.                     echo "<td>Salary sum: ".$sum."</td>";
  100.                     echo "<td>Common town: ".$currTown."</td>";
  101.                     echo "<td>Age: ".number_format($average, 0)."</td>";
  102.                 echo "</tr>";
  103.             ?>
  104.         </tfoot>
  105.     </table>    
  106. </body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment