Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. <?php
  2.  
  3. class db {
  4.  
  5. const hostname = 'localhost';
  6. const user = 'root';
  7. const pass = '';
  8. const dbname = 'ngo';
  9.  
  10. private $dbh;
  11.  
  12. function __construct() {
  13. try {
  14. $conn_string = "mysql:host=" . self::hostname . ";dbname=" . self::dbname;
  15. $this->dbh = new PDO($conn_string, self::user, self::pass);
  16. } catch (PDOException $e) {
  17. echo "ERROR: ";
  18. echo $e->getMessage();
  19. }
  20. }
  21.  
  22. function __destruct() {
  23. $this->dbh = null;
  24. }
  25.  
  26. public function randomString() {
  27. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  28. $randstring = '';
  29. for ($i = 0; $i < 10; $i++) {
  30. $randstring .= $characters[rand(0, strlen($characters) - 1)];
  31. }
  32. return $randstring;
  33. }
  34.  
  35. public function addPatient($fullname, $state, $address, $city, $age, $gender, $norp, $aorp, $phone, $relation, $passport) {
  36. try {
  37. $sql = "INSERT INTO patient(fullname, state,address,city,age,gender,norp,aorp,phone,relation,passport) ";
  38. $sql .= "VALUES ('$fullname','$address','$city','$state','$age','$gender','$norp','$aorp','$phone','$relation','$passport')";
  39. $this->dbh->exec($sql);
  40. } catch (PDOException $e) {
  41. echo "ERROR: ";
  42. echo $e->getMessage();
  43. return false;
  44. }
  45. }
  46.  
  47. public function getFiveSimilar($name) {
  48. $sql = "SELECT id, fullname "
  49. . "FROM patient "
  50. . "WHERE fullname like '$name%' "
  51. . "LIMIT 5";
  52. $pdo = $this->dbh->query($sql);
  53. $n = $pdo->fetchALL(PDO::FETCH_ASSOC);
  54. return $n;
  55. }
  56.  
  57. public function lastPatient() {
  58. $sql = "SELECT id as id "
  59. . "FROM patient "
  60. . "ORDER BY id desc "
  61. . "LIMIT 1";
  62. $pdo = $this->dbh->query($sql);
  63. $n = $pdo->fetch(PDO::FETCH_ASSOC);
  64. return $n['id'];
  65. }
  66.  
  67. public function addVisit($doa, $diagnosis, $prescription, $lastPatient) {
  68. try {
  69. $sql = "INSERT INTO visit(date_entered,date_left,patient_id,diagnosis,prescription) ";
  70. $sql .= "VALUES ('$doa','','$lastPatient','$diagnosis','$prescription')";
  71. $this->dbh->exec($sql);
  72. } catch (PDOException $e) {
  73. echo "ERROR: ";
  74. echo $e->getMessage();
  75. return false;
  76. }
  77. }
  78.  
  79. public function formatDate($date) {
  80. $dt = explode('/', $date);
  81. return $dt[2] . '-' . $dt[1] . '-' . $dt[0];
  82. }
  83.  
  84. public function getSinglePatientData($id) {
  85. $sql = "SELECT * "
  86. . "FROM patient "
  87. . "WHERE id=$id";
  88. $pdo = $this->dbh->query($sql);
  89. $n = $pdo->fetch(PDO::FETCH_ASSOC);
  90. return $n;
  91. }
  92.  
  93. public function getVisits($name, $dateFrom, $dateTo) {
  94. $sql = "SELECT p.id as patientId, p.fullname as fullname, "
  95. . "p.address as address, p.city as city, "
  96. . "p.state as state, p.age as age, "
  97. . "p.gender as gender, p.norp as norp, "
  98. . "p.aorp as aorp, p.phone as phone, p.relation as relation, "
  99. . "p.passport as passport, v.id as visitId, "
  100. . "v.date_entered as dateEntered, v.date_left as dateLeft, "
  101. . "v.diagnosis as diagnosis, v.prescription as prescription "
  102. . "FROM patient p "
  103. . "JOIN visit v "
  104. . "ON p.id=v.patient_id "
  105. . "WHERE fullname LIKE '$name%' "
  106. . "AND v.date_entered>='$dateFrom' "
  107. . "AND v.date_left<='$dateTo'";
  108. $pdo = $this->dbh->query($sql);
  109. $n = $pdo->fetchALL(PDO::FETCH_ASSOC);
  110. return $n;
  111. }
  112.  
  113. public function getVisitsForDownload($name, $dateFrom, $dateTo) {
  114. $sql = "SELECT p.id as patientId, p.fullname as fullname, "
  115. . "p.address as address, p.city as city, "
  116. . "p.state as state, p.age as age, "
  117. . "p.gender as gender, p.norp as norp, "
  118. . "p.aorp as aorp, p.phone as phone, p.relation as relation, "
  119. . "p.passport as passport, v.id as visitId, "
  120. . "v.date_entered as dateEntered, IF(v.date_left='0000-00-00','not finished',v.date_left) as dateLeft, "
  121. . "v.diagnosis as diagnosis, v.prescription as prescription "
  122. . "FROM patient p "
  123. . "JOIN visit v "
  124. . "ON p.id=v.patient_id "
  125. . "WHERE fullname LIKE '$name%' "
  126. . "AND v.date_entered>='$dateFrom' "
  127. . "AND v.date_left<='$dateTo'";
  128. $pdo = $this->dbh->query($sql);
  129. $n = $pdo->fetchALL(PDO::FETCH_ASSOC);
  130. return $n;
  131. }
  132.  
  133. public function finishVisit($date,$id) {
  134. $sql = "UPDATE visit SET date_left='" . $date . "' WHERE id='" . $id . "'";
  135. $pdo = $this->dbh->prepare($sql);
  136. $pdo->execute();
  137. }
  138. }
  139. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement