SHARE
TWEET

Untitled

a guest Apr 27th, 2018 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4. <body>
  5.  
  6. <form method="post">
  7. <!--
  8. Year: <input type="text" name="year"><br>
  9. -->
  10. Choose a date: <input type="number" name="year"><br>
  11. Total or average salary:<br>
  12.  
  13. <select name="option">
  14.     <option value="total">Total</option>
  15.     <option value="average">Average</option>
  16. </select>
  17. <input type="submit">
  18. </form>
  19.  
  20. <?php
  21. $servername = "localhost";
  22. $username = "root";
  23. $password = "root";
  24. $dbname = "employees";
  25.  
  26. // Create connection
  27. $conn = new mysqli($servername, $username, $password, $dbname);
  28.    
  29.     if(!$conn){
  30.         exit("Error: could not reach database.");
  31.     }
  32. // Check connection
  33. if ($conn->connect_error) {
  34.     die("Connection failed: " . $conn->connect_error);
  35. }
  36.  
  37.    
  38.  
  39. // $year = $_POST['year']
  40. $year = $_POST['year'];
  41. $option = $_POST['option'];
  42.  
  43.  
  44. $minyear = "SELECT MIN(LEFT(s.from_date,4))
  45.             FROM salaries AS s
  46.             GROUP BY s.from_date ASC";
  47.  
  48.    
  49. if($option == 'total'){
  50.     $sql = "SELECT SUM(s.salary) AS salary
  51.             FROM salaries AS s
  52.             WHERE $year = LEFT(s.from_date,4)";
  53. } else{
  54.     $sql = "SELECT SUM(s.salary)/COUNT(s.salary) AS salary
  55.             FROM salaries AS s
  56.             WHERE $year = LEFT(s.from_date,4)";
  57. }
  58.  
  59.  
  60. $resultat = mysqli_query($conn, $sql);
  61. $minyearresultat = mysqli_query($conn, $minyear);
  62.  
  63. print('<table border="1">');
  64. $rad = mysqli_fetch_assoc($resultat);
  65.     if(min($minyearresultat) > $year){
  66.         echo ("Please input correct year.");
  67.     }else if ($resultat->num_rows > 0) {
  68.     while($rad){
  69.         $salary=$rad['salary'];
  70.         echo ("<tr><td>$year</td><td>$salary</td></tr>");
  71.         $rad = mysqli_fetch_assoc($resultat);
  72.         }
  73.     }else{
  74.         echo "0 results.";  
  75.     }
  76.     print('</table>');
  77.  
  78.     mysqli_close($conn);
  79.    
  80. ?>
  81.  
  82.  
  83.     </body>
  84. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top