Skip to content

Instantly share code, notes, and snippets.

@d0peCode
Created August 31, 2018 13:20
Show Gist options
  • Save d0peCode/bbb4e086dac97ebd9db8d6badca509d8 to your computer and use it in GitHub Desktop.
Save d0peCode/bbb4e086dac97ebd9db8d6badca509d8 to your computer and use it in GitHub Desktop.
<?php
public function searchOffer($query, $params) {
$stmt = $this->conn->prepare($query);
var_dump($params);
$stmt->bindParam($params);
$return = $stmt->execute();
if($return) return $stmt;
return false;
}
<?php
class Database
{
// specify your own database credentials
private $host = "localhost";
private $db_name = "twohouse_db";
private $username = "root";
private $password = "";
public $conn;
// get the database connection
public function getConnection()
{
$this->conn = null;
try
{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->exec("set names utf8");
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
<?php
// required headers
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: *");
header("Content-Type: application/json");
header("Access-Control-Allow-Credentials: true");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS')
{
http_response_code(200);
die();
}
$query = 'SELECT * FROM buildings WHERE ';
$whereOr = [];
$whereAnd = [];
$params = [];
/* Where's OR */
if(isset($_POST['query'])) {
$whereOr[] = 'name LIKE :query';
$params[':query'] = '%'.$_POST['query'].'%';
}
if(isset($_POST['type'])) {
$whereOr[] = 'type = :type';
$params[':type'] = $_POST['type'];
}
if(isset($_POST['garage'])) {
$whereOr[] = 'garage = :garage';
$params[':garage'] = $_POST['garage'];
}
if(isset($_POST['balcony'])) {
$whereOr[] = 'balcony = :balcony';
$params[':balcony'] = $_POST['balcony'];
}
if(isset($_POST['garden'])) {
$whereOr[] = 'garden = :garden';
$params[':garden'] = $_POST['garden'];
}
if(isset($_POST['parking'])) {
$whereOr[] = 'parking = :parking';
$params[':parking'] = $_POST['parking'];
}
if(isset($_POST['new'])) {
$whereOr[] = 'new = :new';
$params[':new'] = $_POST['new'];
}
if(isset($_POST['elevator'])) {
$whereOr[] = 'elevator = :elevator';
$params[':elevator'] = $_POST['elevator'];
}
/* Where's AND */
if(isset($_POST['price_min'])) {
$whereAnd[] = 'price >= :price_min';
$params[':price_min'] = $_POST['price_min'];
}
if(isset($_POST['price_max'])) {
$whereAnd[] = 'price <= :price_max';
$params[':price_max'] = $_POST['price_max'];
}
if(isset($_POST['price_meter_min'])) {
$whereAnd[] = 'priceMeter >= :price_meter_min';
$params[':price_meter_min'] = $_POST['price_meter_min'];
}
if(isset($_POST['price_meter_max'])) {
$whereAnd[] = 'priceMeter <= :price_meter_max';
$params[':price_meter_max'] = $_POST['price_meter_max'];
}
if(isset($_POST['square_min'])) {
$whereAnd[] = 'square >= :square_min';
$params[':square_min'] = $_POST['square_min'];
}
if(isset($_POST['square_max'])) {
$whereAnd[] = 'square <= :square_max';
$params[':square_max'] = $_POST['square_max'];
}
if(isset($_POST['year_min'])) {
$whereAnd[] = 'year >= :year_min';
$params[':year_min'] = $_POST['year_min'];
}
if(isset($_POST['year_max'])) {
$whereAnd[] = 'year <= :year_max';
$params[':year_max'] = $_POST['year_max'];
}
if(isset($_POST['rooms_min'])) {
$whereAnd[] = 'roomsCount >= :rooms_min';
$params[':rooms_min'] = $_POST['rooms_min'];
}
if(isset($_POST['rooms_max'])) {
$whereAnd[] = 'roomsCount <= :rooms_max';
$params[':rooms_max'] = $_POST['rooms_max'];
}
if(isset($_POST['floors_min'])) {
$whereAnd[] = 'floorsCount >= :floor_min';
$params[':floors_min'] = $_POST['floors_min'];
}
if(isset($_POST['floors_max'])) {
$whereAnd[] = 'floorsCount <= :floors_max';
$params[':floors_max'] = $_POST['floors_max'];
}
/* combain where's */
if(!empty($whereOr)){
$query .= implode(' OR ', $whereOr);
}
if(!empty($whereAnd)){
$query .= ' AND '.implode(' AND ', $whereAnd);
}
if(!empty($whereAnd) || !empty($whereAnd)){
require_once '../config/database.php';
require_once '../classes/building.php';
$database = new Database();
$db = $database->getConnection();
$building = new Building($db);
$stmt = $building->searchOffer($query, $params);
if($stmt)
{
$num = $stmt->rowCount();
if($num > 0) {
$buildings_arr=array();
$buildings_arr["record"]=array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
$building_item=array(
"id" => $id,
"name" => $name
);
array_push($buildings_arr["record"], $building_item);
}
echo json_encode($buildings_arr);
}
else
{
echo json_encode(
array("message" => "No building found.")
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment