Guest User

Untitled

a guest
Jul 11th, 2018
1,392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. // admindb.php
  2.  
  3. <?php
  4.  
  5. function add_admin($email,$password){
  6. $db = Database::getDB();
  7. $hash = password_hash($password, PASSWORD_DEFAULT);
  8. $query = 'insert into administrators (email, password) values (:email, :password)';
  9. $statement = $db->prepare($query);
  10. $statement->bindValue(':email', $email);
  11. $statement->bindValue(':password', $hash);
  12. $statement->execute();
  13. $statement->closeCursor();
  14. }
  15.  
  16. ?>
  17.  
  18. // database.php
  19.  
  20. <?php
  21. class Database {
  22. private static $dsn = "mysql:host=localhost;dbname=real_estate";
  23. private static $username = "user";
  24. private static $password = "password";
  25. private static $db;
  26.  
  27. private function __construct(){}
  28.  
  29. public static function getDB() {
  30. if(!isset(self::$db)){
  31. try {
  32. self::$db = new PDO(self::$dsn, self::$username, self::$password);
  33. } catch (PDOException $e) {
  34. $error_message = $e->getMessage();
  35. include ('../errors/error.php');
  36. exit();
  37. }
  38.  
  39. }
  40. return self::$db;
  41. }
  42. }
  43.  
  44. ?>
  45.  
  46. // homedb.php
  47.  
  48. <?php
  49.  
  50. function gethomes($city, $bdrms, $baths, $min, $max){
  51. $db = Database::getDB();
  52. if ($city) {
  53. $query = "select * from homes where city = :city";
  54. $statement = $db->prepare($query);
  55. $statement->bindValue(':city', $city);
  56. $statement->execute();
  57. $homes = $statement->fetchAll();
  58. $statement->closeCursor();
  59. if ($city && $bdrms) {
  60. $query = 'select * from homes where city = :city and bdrms >= :bdrms';
  61. $statement = $db->prepare($query);
  62. $statement->bindValue(':city', $city);
  63. $statement->bindValue(':bdrms', $bdrms);
  64. $statement->execute();
  65. $homes = $statement->fetchAll();
  66. $statement->closeCursor();
  67. if ($city && $bdrms && $baths) {
  68. $query = 'select * from homes where city = :city and bdrms >= :bdrms and baths >= :baths';
  69. $statement = $db->prepare($query);
  70. $statement->bindValue(':city', $city);
  71. $statement->bindValue(':bdrms', $bdrms);
  72. $statement->bindValue(':baths', $baths);
  73. $statement->execute();
  74. $homes = $statement->fetchAll();
  75. $statement->closeCursor();
  76. if ($city && $bdrms && $baths && $min) {
  77. $query = 'select * from homes where city = :city and bdrms >= :bdrms and baths >= :baths and price >= :min';
  78. $statement = $db->prepare($query);
  79. $statement->bindValue(':city', $city);
  80. $statement->bindValue(':bdrms', $bdrms);
  81. $statement->bindValue(':baths', $baths);
  82. $statement->bindValue(':min', $min);
  83. $statement->execute();
  84. $homes = $statement->fetchAll();
  85. $statement->closeCursor();
  86. // the other code
  87. }
  88. }
  89. }
  90. } else if ($city && $bdrms && $baths && $min && $max) {
  91. $query = 'select * from homes where city = :city and bdrms >= :bdrms and baths >= :baths and price >= :min and price <= :max';
  92. $statement = $db->prepare($query);
  93. $statement->bindValue(':city', $city);
  94. $statement->bindValue(':bdrms', $bdrms);
  95. $statement->bindValue(':baths', $baths);
  96. $statement->bindValue(':min', $min);
  97. $statement->bindValue(':max', $max);
  98. $statement->execute();
  99. $homes = $statement->fetchAll();
  100. $statement->closeCursor();
  101. if ($min && $max) {
  102. $query = 'select * from homes where price >= :min';
  103. $statement = $db->prepare($query);
  104. $statement->bindValue(':min', $min);
  105. $statement->execute();
  106. $homes = $statement->fetchAll();
  107. $statement->closeCursor();
  108. }
  109. } else if ($min) {
  110. $query = 'select * from homes where price >= :min';
  111. $statement = $db->prepare($query);
  112. $statement->bindValue(':min', $min);
  113. $statement->execute();
  114. $homes = $statement->fetchAll();
  115. $statement->closeCursor();
  116. if ($max) {
  117. $query = 'select * from homes where price >= :max';
  118. $statement = $db->prepare($query);
  119. $statement->bindValue(':max', $max);
  120. $statement->execute();
  121. $homes = $statement->fetchAll();
  122. $statement->closeCursor();
  123. }
  124. }
  125. return $homes;
  126. }
  127.  
  128. function getAllHomes(){
  129. $db = Database::getDB();
  130. $query = 'select * from homes';
  131. $statement = $db->prepare($query);
  132. $statement->execute();
  133. $homes = $statement->fetchAll();
  134. $statement->closeCursor();
  135. return $homes;
  136. }
  137.  
  138. ?>
  139.  
  140. // index.php (main)
  141.  
  142. <?php
  143.  
  144. require_once('model/database.php');
  145. require_once('model/homedb.php');
  146.  
  147. $action = filter_input(INPUT_POST, 'action');
  148. if ($action == NULL) {
  149. $action = filter_input(INPUT_GET, 'action');
  150. if ($action == NULL){
  151. $action = 'homeapp';
  152. }
  153. }
  154.  
  155. switch($action){
  156. case 'homeapp':
  157. $homes = getAllHomes();
  158. include 'homes/homelist.php';
  159. break;
  160. case 'results':
  161. $city = filter_input(INPUT_POST, 'city');
  162. $bdrms = filter_input(INPUT_POST, 'bdrms');
  163. $baths = filter_input(INPUT_POST, 'baths');
  164. $min = filter_input(INPUT_POST, 'min');
  165. $max = filter_input(INPUT_POST, 'max');
  166. $homes = gethomes($city, $bdrms, $baths, $min, $max);
  167. $rowsperpage = 8;
  168. $numrows = count($homes);
  169. $totalpages = ceil($numrows/$rowsperpage);
  170. include 'homes/results.php';
  171. break;
  172. }
  173.  
  174. ?>
  175.  
  176. <?php foreach ($result as $home): ?>
  177.  
  178. <?php echo $home['home_id'] . '.jpg'; ?>
  179. <?php echo $home['sqr_ft'] ?>
  180. <?php echo $home['garages'] ?>
  181. <?php echo $home['bdrms'] ?>
  182. <?php echo $home['baths'] ?>
  183.  
  184. <?php endforeach; ?>
Add Comment
Please, Sign In to add comment