Advertisement
Guest User

poshua hales

a guest
Feb 18th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. <?php
  2.  
  3. class EmployeeTableGateway {
  4.  
  5. //connection object
  6. private $connection;
  7.  
  8. public function __construct($c) {
  9. $this->connection = $c;
  10. }
  11.  
  12. //--query to get all employees from db and order them in desired way according to parametor--
  13. public function getEmployees($sortOrder) {
  14. $sqlQuery = "SELECT * FROM employee ORDER BY " . $sortOrder; //the query
  15. $statement = $this->connection->prepare($sqlQuery); //getting statement reaady
  16. $status = $statement->execute(); //executing the query
  17.  
  18. if (!$status) { //if no response theres an error
  19. die("Could not retrieve employee");
  20. }
  21. return $statement;
  22. }
  23.  
  24. //--query to get specific employee from db by their id--
  25. public function getEmployeeByID($id) {
  26. $sqlQuery = "SELECT * FROM employee WHERE id = :id"; //the query
  27. $statement = $this->connection->prepare($sqlQuery); //getting statement reaady
  28. $params = array(
  29. "id" => $id //setting value of placeholder
  30. );
  31. $status = $statement->execute($params); //executing the query
  32.  
  33. if (!$status) { //if no response theres an error
  34. die("Could not retrieve employee");
  35. }
  36. return $statement;
  37. }
  38.  
  39. //--query to get specific employee from db by their username--
  40. public function getEmployeeByUsername($username) {
  41. $sqlQuery = 'SELECT * FROM employee WHERE username = "' . $username . '"'; //the query
  42. $statement = $this->connection->query($sqlQuery); //getting statement reaady
  43. if (!$statement) {
  44. die('Could not retrieve employee details'); //if no response theres an error
  45. }
  46. return $statement;
  47. }
  48. //--query to insert an employee into db--
  49. public function insertEmployee($n, $e, $u, $p) {
  50. $sqlQuery = "INSERT INTO employee " . //the query
  51. "(name, email, username, password) " .
  52. "VALUES (:name, :email , :username, :password)";
  53.  
  54. $statement = $this->connection->prepare($sqlQuery);
  55. $params = array( //setting values of placeholders
  56. "name" => $n,
  57. "email" => $e,
  58. "username" => $u,
  59. "password" => $p
  60. );
  61.  
  62. $status = $statement->execute($params); //executing the query
  63.  
  64. if (!$status) { //if no response theres an error
  65. die("Could not insert employee");
  66. }
  67.  
  68. $id = $this->connection->lastInsertId();
  69. // need to add in event id
  70. return $id;
  71. }
  72. //--query to delete an employee from the db--
  73. public function deleteEmployee($id) {
  74. $sqlQuery = "DELETE FROM employee WHERE id = :id"; //the query
  75.  
  76. $statement = $this->connection->prepare($sqlQuery); //getting statement reaady
  77. $params = array( //setting value of placeholder
  78. "id" => $id
  79. );
  80.  
  81. $status = $statement->execute($params); //executing the query
  82.  
  83. if (!$status) {
  84. die("Could not delete employee");
  85. }
  86.  
  87. return ($statement->rowCount() == 1);
  88. }
  89. //--query to update an employee--
  90. public function updateEmployees($eID, $n, $e, $u, $p) {
  91. $sqlQuery = "UPDATE employee SET " . //the query
  92. "name = :name, " .
  93. "email = :email, " .
  94. "username = :username, " .
  95. "password = :password " .
  96. " WHERE id = :id";
  97.  
  98. $statement = $this->connection->prepare($sqlQuery); //getting statement reaady
  99. $params = array( //setting value of placeholder
  100. "id" => $eID,
  101. "name" => $n,
  102. "email" => $e,
  103. "username" => $u,
  104. "password" => $p
  105. );
  106.  
  107. $status = $statement->execute($params); //executing the query
  108.  
  109. if (!$status) { //if no response theres an error
  110. die("Could not update employee");
  111. }
  112. return ($statement->rowCount() == 1);
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement