SHARE
TWEET

Untitled

a guest Apr 27th, 2018 81 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">
  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 DESC");
  47.    
  48. if($option == 'total'){
  49.     $sql = "SELECT SUM(s.salary) AS salary
  50.             FROM salaries AS s
  51.             WHERE $year = LEFT(s.from_date,4)";
  52. } else{
  53.     $sql = "SELECT SUM(s.salary)/COUNT(s.salary) AS salary
  54.             FROM salaries AS s
  55.             WHERE $year = LEFT(s.from_date,4)";
  56. }
  57.  
  58.  
  59. $resultat = mysqli_query($conn, $sql);
  60.  
  61. print('<table border="1">');
  62. $rad = mysqli_fetch_assoc($resultat);
  63.     if($minyear > $year){
  64.         echo ("Please input correct year.");
  65.     }else if ($resultat->num_rows > 0) {
  66.     while($rad){
  67.         $salary=$rad['salary'];
  68.         echo ("<tr><td>$year</td><td>$salary</td></tr>");
  69.         $rad = mysqli_fetch_assoc($resultat);
  70.         }
  71.     }else{
  72.         echo "0 results.";  
  73.     }
  74.     print('</table>');
  75.  
  76.     mysqli_close($conn);
  77.    
  78. ?>
  79.  
  80.  
  81.     </body>
  82. </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