Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // admindb.php
- <?php
- function add_admin($email,$password){
- $db = Database::getDB();
- $hash = password_hash($password, PASSWORD_DEFAULT);
- $query = 'insert into administrators (email, password) values (:email, :password)';
- $statement = $db->prepare($query);
- $statement->bindValue(':email', $email);
- $statement->bindValue(':password', $hash);
- $statement->execute();
- $statement->closeCursor();
- }
- ?>
- // database.php
- <?php
- class Database {
- private static $dsn = "mysql:host=localhost;dbname=real_estate";
- private static $username = "user";
- private static $password = "password";
- private static $db;
- private function __construct(){}
- public static function getDB() {
- if(!isset(self::$db)){
- try {
- self::$db = new PDO(self::$dsn, self::$username, self::$password);
- } catch (PDOException $e) {
- $error_message = $e->getMessage();
- include ('../errors/error.php');
- exit();
- }
- }
- return self::$db;
- }
- }
- ?>
- // homedb.php
- <?php
- function gethomes($city, $bdrms, $baths, $min, $max){
- $db = Database::getDB();
- if ($city) {
- $query = "select * from homes where city = :city";
- $statement = $db->prepare($query);
- $statement->bindValue(':city', $city);
- $statement->execute();
- $homes = $statement->fetchAll();
- $statement->closeCursor();
- if ($city && $bdrms) {
- $query = 'select * from homes where city = :city and bdrms >= :bdrms';
- $statement = $db->prepare($query);
- $statement->bindValue(':city', $city);
- $statement->bindValue(':bdrms', $bdrms);
- $statement->execute();
- $homes = $statement->fetchAll();
- $statement->closeCursor();
- if ($city && $bdrms && $baths) {
- $query = 'select * from homes where city = :city and bdrms >= :bdrms and baths >= :baths';
- $statement = $db->prepare($query);
- $statement->bindValue(':city', $city);
- $statement->bindValue(':bdrms', $bdrms);
- $statement->bindValue(':baths', $baths);
- $statement->execute();
- $homes = $statement->fetchAll();
- $statement->closeCursor();
- if ($city && $bdrms && $baths && $min) {
- $query = 'select * from homes where city = :city and bdrms >= :bdrms and baths >= :baths and price >= :min';
- $statement = $db->prepare($query);
- $statement->bindValue(':city', $city);
- $statement->bindValue(':bdrms', $bdrms);
- $statement->bindValue(':baths', $baths);
- $statement->bindValue(':min', $min);
- $statement->execute();
- $homes = $statement->fetchAll();
- $statement->closeCursor();
- // the other code
- }
- }
- }
- } else if ($city && $bdrms && $baths && $min && $max) {
- $query = 'select * from homes where city = :city and bdrms >= :bdrms and baths >= :baths and price >= :min and price <= :max';
- $statement = $db->prepare($query);
- $statement->bindValue(':city', $city);
- $statement->bindValue(':bdrms', $bdrms);
- $statement->bindValue(':baths', $baths);
- $statement->bindValue(':min', $min);
- $statement->bindValue(':max', $max);
- $statement->execute();
- $homes = $statement->fetchAll();
- $statement->closeCursor();
- if ($min && $max) {
- $query = 'select * from homes where price >= :min';
- $statement = $db->prepare($query);
- $statement->bindValue(':min', $min);
- $statement->execute();
- $homes = $statement->fetchAll();
- $statement->closeCursor();
- }
- } else if ($min) {
- $query = 'select * from homes where price >= :min';
- $statement = $db->prepare($query);
- $statement->bindValue(':min', $min);
- $statement->execute();
- $homes = $statement->fetchAll();
- $statement->closeCursor();
- if ($max) {
- $query = 'select * from homes where price >= :max';
- $statement = $db->prepare($query);
- $statement->bindValue(':max', $max);
- $statement->execute();
- $homes = $statement->fetchAll();
- $statement->closeCursor();
- }
- }
- return $homes;
- }
- function getAllHomes(){
- $db = Database::getDB();
- $query = 'select * from homes';
- $statement = $db->prepare($query);
- $statement->execute();
- $homes = $statement->fetchAll();
- $statement->closeCursor();
- return $homes;
- }
- ?>
- // index.php (main)
- <?php
- require_once('model/database.php');
- require_once('model/homedb.php');
- $action = filter_input(INPUT_POST, 'action');
- if ($action == NULL) {
- $action = filter_input(INPUT_GET, 'action');
- if ($action == NULL){
- $action = 'homeapp';
- }
- }
- switch($action){
- case 'homeapp':
- $homes = getAllHomes();
- include 'homes/homelist.php';
- break;
- case 'results':
- $city = filter_input(INPUT_POST, 'city');
- $bdrms = filter_input(INPUT_POST, 'bdrms');
- $baths = filter_input(INPUT_POST, 'baths');
- $min = filter_input(INPUT_POST, 'min');
- $max = filter_input(INPUT_POST, 'max');
- $homes = gethomes($city, $bdrms, $baths, $min, $max);
- $rowsperpage = 8;
- $numrows = count($homes);
- $totalpages = ceil($numrows/$rowsperpage);
- include 'homes/results.php';
- break;
- }
- ?>
- <?php foreach ($result as $home): ?>
- <?php echo $home['home_id'] . '.jpg'; ?>
- <?php echo $home['sqr_ft'] ?>
- <?php echo $home['garages'] ?>
- <?php echo $home['bdrms'] ?>
- <?php echo $home['baths'] ?>
- <?php endforeach; ?>
Add Comment
Please, Sign In to add comment