Skip to content

In progress. Basic functionality is there. #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions ajaxCalls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//handles adding new property and editing existing ones
function addNewProperty(){
var address = $("#address").val() || '';
var city = $("#city").val() || '';
var state = $("#state").val() || '';
var zip = $("#zip").val() || '';
var existing_id = $("#existing_id").val() || 0;
var salesAndDates= [[]];
var count = $('.sale-section').length; //how many so far in list
var price, date;

for(var i=0; i<count; i++){
price = $("#price_"+i).val() || '';
date = $("#date_"+i).val() || '';
salesAndDates[i][0] = price;
salesAndDates[i][1] = date;
}

var salesAsString = JSON.stringify(salesAndDates);

$.post('postFunctions.php', { do: 'addProperty',
id: existing_id,
address: address,
city: city,
state: state,
zip: zip,
data: salesAsString,
dataType: 'json'}, function(id) {
if(id == 0) {
alert("That address already exists.");
}
else{
$('form').get(0).reset();
var detailButton = "<button type='button' class= 'btn btn-primary btn-xs' data-toggle='modal' data-target='#addNewProperty' onClick='allInfo("+id+");'>View Details</button>";
var deleteButton = "<button type='button' class= 'btn btn-danger btn-xs' onClick='deleteProperty("+id+");'>Delete</button>";

if(existing_id != 0){ //refresh that row
$('#'+existing_id).get()[0].innerHTML = '<td>'+address+'</td><td>'+city+'</td><td>'+state+'</td><td>'+zip+'</td><td>'+detailButton+'</td><td>'+deleteButton+'</td>';
}
else{ //add it
$('table').append('<tr id='+id+' ><td>'+address+'</td><td>'+city+'</td><td>'+state+'</td><td>'+zip+'</td><td>'+detailButton+'</td><td>'+deleteButton+'</td></tr>');
}
}
});
}

function deleteProperty(property_id){
$.post('postFunctions.php', { do: 'deleteProperty',
property_id: property_id});
$('#'+property_id).remove();
}

function deleteSale(sale_id, inDB){
if(inDB){
$.post('postFunctions.php', { do: 'deleteSale',
sale_id: sale_id});
$('#'+sale_id).remove();
}else{
$('#sale_'+sale_id).remove();
}
}

//get all the info for a specific property
function allInfo(property_id){
//clear out the modal
$('form').get(0).reset();
$('.sale-section').remove();

//populate with data
$.post('postFunctions.php', { do: 'allInfo',
property_id: property_id}, function(data){
var allInfo = JSON.parse(data);
$('#address').val(allInfo[0]['address']);
$('#city').val(allInfo[0]['city']);
$('#state').val(allInfo[0]['state']);
$('#zip').val(allInfo[0]['zip']);
$('#existing_id').val(property_id);

if(allInfo[0]['sale_price'] || allInfo[0]['sale_date'] ){
allInfo.forEach((sale, index) => {
var saleInputRows = "<div class='sale-section' id="+allInfo[index]['sale_id']+">"+
"<label >Sale Price:</label><input class='sales' id='price_"+index+" placeholder='Sale Price' value="+allInfo[index]['sale_price']+"></input>"+
"<label >Sale Date:</label><input class='sales' id='date_"+index+" placeholder='Sale Date' value="+allInfo[index]['sale_date']+"></input>"+
"<button type='button' class='btn btn-primary btn-xs' onClick='deleteSale("+allInfo[index]['sale_id']+",1);' >Delete Sale</button>"+
"</div>";
$('form').append(saleInputRows);
});
}
});
}

function addSaleRow(){
var count = $('.sale-section').length; //how many so far in list
var saleInputRows = "<div class='sale-section' id='sale_"+count+"'>"+
"<label >Sale Price:</label><input class='sales' id='price_"+count+"' placeholder='Sale Price' ></input>"+
"<label >Sale Date:</label><input class='sales' id='date_"+count+"' placeholder='Sale Date' ></input>"+
"<button type='button' class='btn btn-primary btn-xs' onClick='deleteSale("+count+", 0);' >Delete Sale</button>"+
"</div>";
$('form').append(saleInputRows);

}


148 changes: 148 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

//TODO, could add in future
//landing page/login
//option to view all properties or search
//option to filter
//order the results better
//map view
//confirmation on delete
//confirmation on save/ update
//also double check first if that's what they mean to click, and notify what sales also deleting
//add a datepicker to help with sale date format


//****NOTE: change $user and $password for your database
$user = 'root';
$password = '';

session_start();

$pdo = new PDO("mysql:host=localhost", $user, $password);
$pdo->exec("USE nwick_php_test");

//if this is a new session, create the database and some test data
if(!isset($_SESSION['user'])){
try {
$pdo = new PDO("mysql:host=localhost", $user, $password);
$pdo->exec("CREATE DATABASE IF NOT EXISTS nwick_php_test");
$pdo->exec("USE nwick_php_test");

} catch (PDOException $e) {echo 'error';
die("DB ERROR: ". $e->getMessage());
}

$pdo->exec("CREATE TABLE IF NOT EXISTS properties (property_id INT NOT NULL AUTO_INCREMENT, address VARCHAR(256), city VARCHAR(256), state VARCHAR(2), zip varchar(10), PRIMARY KEY (property_id))");
$pdo->exec("CREATE TABLE IF NOT EXISTS sales (sale_id INT NOT NULL AUTO_INCREMENT, property_id INT, sale_date DATE, sale_price DECIMAL(10,2), PRIMARY KEY (sale_id))");

//simple test data
for($i = 0; $i<10; $i++){
$insert = $pdo->prepare("INSERT INTO properties VALUES (0, :address, :city, :state, :zip);");
$insert->execute(["address"=> "12".$i." Main St", "city"=>"San Diego", "state"=>"CA", "zip"=>"92101"]);
}

$_SESSION['user'] = 'test_user';
}

$propertyTable = "<table id='propertyTable'><tr><th>Address</th><th>City</th><th 1>State</th><th colspan=1>Zip</th><th colspan=2></th><th colspan=2></th></tr>";
$propertyResults = $pdo->query("SELECT * FROM properties ORDER BY property_id");

while($row = $propertyResults->fetch() ) {
//don't display the id
$detailsButton = "<button type='button' class= 'btn btn-primary btn-xs' data-toggle='modal' data-target='#addNewProperty' onClick='allInfo({$row[0]})'>View Details</button>";
$deleteButton = "<button type='button' class= 'btn btn-danger btn-xs' onClick='deleteProperty({$row[0]})'>Delete</button>";
$propertyTable .= "<tr id={$row[0]} ><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$detailsButton</td><td>$deleteButton</td></tr>";
}

echo $propertyTable;
echo "<button type='button' class= 'btn btn-primary' data-toggle='modal' data-target='#addNewProperty' id='addNew' >Add New Property</button>";

?>


<!DOCTYPE HTML>
<html>
<head>
<title>Property Sales</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script type="text/javascript" src="ajaxCalls.js"></script>

<style>
table{
border: 1px solid black;
border-bottom-right-radius:20px ;
margin: auto;
}

tr{
border: 1px solid grey;
}

th{
background-color:skyblue;
text-align:left;
padding:10px;
}

td{
margin:10px;
padding:10px;
text-align:left;
}

#addNew{
margin-left:80%;
margin-top:20px;
margin-right:20px;
}

.sales{
width: 100px;
border-radius:5px;
margin: 10px;
}

#saleRowButton{
margin:10px 10px 10px 15px;
}
</style>


</head>
<body>

<!~~ Modal ~~>
<div class="modal fade" id="addNewProperty" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Property And Sales Info</h4>
</div>
<button id='saleRowButton' type="button" class="btn btn-primary btn-xs" onClick='addSaleRow();' >Add Sale</button>
<div class="modal-body">
<form name='newPropertyForm' >
<div class="form-group">
<label>Address:</label>
<input class="form-control short" id='address' name='address' placeholder='Address' ></input>
<input class="form-control" id='city' name='city' placeholder='City' ></input>
<input class="form-control" id='state' name='state' placeholder='State' ></input>
<input class="form-control" id='zip' name='zip' placeholder='Zip' ></input>
<input id='existing_id' class='invisible' ></input>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success" data-dismiss="modal" onClick='addNewProperty();' >Save</button>
</div>
</div>
</div>
</div>


</body>
</html>

103 changes: 103 additions & 0 deletions postFunctions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

$pdo = new PDO("mysql:host=localhost", 'root', '');
$pdo->exec("USE nwick_php_test");

switch ($_POST['do']){

case 'addProperty': var_dump($_POST);
//Also handles editing.
//if property already has an id, they're just editing it. Do updates.
if($_POST['id'] != 0){
$sql = "UPDATE properties SET address=:address, city=:city, state=:state, zip=:zip WHERE property_id = :property_id";
$update = $pdo->prepare($sql);
$update->execute(["address"=> $_POST['address'], "city"=>$_POST['city'], "state"=>$_POST['state'], "zip"=>$_POST['zip'], "property_id"=> $_POST['id']]);

//take care of sales. just delete old ones and insert what's there now.
$sql = "DELETE FROM sales WHERE property_id = :property_id";
$delete = $pdo->prepare($sql);
$delete->execute(["property_id"=>$_POST['id']]);
// var_dump($_POST);
// var_dump(json_decode($_POST['data']));
$salesAndDates = json_decode($_POST['data']);

if($salesAndDates){

foreach($salesAndDates as $sale){
$sql = "INSERT INTO sales VALUES ('',:property_id, :sale_date, :sale_price)";
$insert = $pdo->prepare($sql);
$insert->execute(["property_id"=>$_POST['id'], "sale_date"=>$sale[1], "sale_price"=>$sale[0]]);
}
}
echo $_POST['id'];
}
else{
//Otherwise, create new property
$sql = "SELECT COUNT(*) FROM properties WHERE address = :address AND city=:city AND state=:state AND zip= :zip";
$duplicateCheck = $pdo->prepare($sql);
$duplicateCheck->execute(["address"=> $_POST['address'], "city"=>$_POST['city'], "state"=>$_POST['state'], "zip"=>$_POST['zip']]);
$count = $duplicateCheck->fetch()[0][0];

if($count){
echo 0; //address already exists, error out
}else{
//create new property
$sql = "INSERT INTO properties VALUES (0,:address, :city, :state, :zip)";
$insert = $pdo->prepare($sql);
$insert->execute(["address"=> $_POST['address'], "city"=>$_POST['city'], "state"=>$_POST['state'], "zip"=>$_POST['zip']]);

$result = $pdo->query("SELECT MAX(property_id) FROM properties");
$id = $result->fetch()[0];

$salesAndDates = json_decode($_POST['data']);

if($salesAndDates){
foreach($salesAndDates as $sale){
$sql = "INSERT INTO sales VALUES ('',:property_id, :sale_date, :sale_price)";
$insert = $pdo->prepare($sql);
$insert->execute(["property_id"=>$id, "sale_date"=>$sale[1], "sale_price"=>$sale[0]]);
}
}

echo $id;
}
}
break;


case 'deleteProperty':
//delete property. cascades to delete assoc sales
$sql = "DELETE FROM properties WHERE property_id = :property_id";
$delete = $pdo->prepare($sql);
$delete->execute(["property_id"=> $_POST['property_id']]);

$sql = "DELETE FROM sales WHERE property_id = :property_id";
$delete = $pdo->prepare($sql);
$delete->execute(["property_id"=> $_POST['property_id']]);
break;

case 'deleteSale':
//delete a single sale
$sql = "DELETE FROM sales WHERE sale_id = :sale_id";
$delete = $pdo->prepare($sql);
$delete->execute(["sale_id"=> $_POST['sale_id']]);
break;

case 'allInfo':
//get all sale and property info
$sql = "SELECT p.property_id,p.address,p.city,p.state,p.zip, s.sale_id, s.sale_date, s.sale_price
FROM properties p
LEFT JOIN sales s ON s.property_id = p.property_id
WHERE p.property_id = :property_id
ORDER BY s.sale_date";
$allInfo = $pdo->prepare($sql);
$allInfo->execute(["property_id"=> $_POST['property_id']]);
echo json_encode($allInfo->fetchAll(PDO::FETCH_ASSOC));
break;

default:
break;

}

?>