Time for action - creating the form to add property

  1. Create a file /path/to/webroot/properties/add.php with the following content:
    <?php
    /**
    * File: /path/to/webroot/properties/add.php
    */
    include('init.php'),
    // Get the list of cities
    $query = "SELECT id, name FROM cities";
    foreach ($dbh->query($query) as $row) {
    $viewVars['cities'][$row['id']] = $row['name'];
    }
    // Get the list of localities
    $query = "SELECT id, name FROM localities";
    foreach ($dbh->query($query) as $row) {
    $viewVars['localities'][$row['id']] = $row['name'];
    }
    // Get the list of amenities
    $query = "SELECT id, name FROM amenities";
    foreach ($dbh->query($query) as $row) {
    $viewVars['amenities'][$row['id']] = $row['name'];
    }
    // If form is submitted then save the data
    // We aren't doing any validation but in a real
    // web application you should.
    if (!empty($_POST['description'])) {
    // Query to insert the property
    $query = "INSERT INTO
    properties
    SET
    type = :type,
    transaction_type = :transaction_type,
    description = :description,
    price = :price,
    city_id = :city_id,
    address = :address,
    zip_code = :zip_code,
    bedrooms = :bedrooms,
    bathrooms = :bathrooms,
    area = :area,
    built_year = :built_year,
    latitude = :latitude,
    longitude = :longitude,
    date_added = :date_added";
    $stmt = $dbh->prepare($query);
    $params = array(
    ':type' => strip_tags($_POST['type']),
    ':transaction_type' =>
    strip_tags($_POST['transaction_type']),
    ':description' => strip_tags($_POST['description']),
    ':price' => (int)$_POST['price'],
    ':city_id' => (int)$_POST['city_id'],
    ':address' => strip_tags($_POST['address']),
    ':zip_code' => strip_tags($_POST['zip_code']),
    ':bedrooms' => (int)$_POST['bedrooms'],
    ':bathrooms' => (int)$_POST['bathrooms'],
    ':area' => (float)$_POST['area'],
    ':built_year' => (int)$_POST['built_year'],
    ':latitude' => (float)$_POST['latitude'],
    ':longitude' => (float)$_POST['longitude'],
    ':date_added' => date('Y-m-d H:i:s'),
    );
    // Execute the statement
    $stmt->execute($params);
    // Get the property id to be used for related amenities
    $property_id = $dbh->lastInsertId();
    // Insert the amenities
    foreach ($_POST['amenities'] as $amenity) {
    $query = "INSERT INTO
    amenities_properties
    SET
    amenity_id = :amenity_id,
    property_id = :property_id";
    $stmt = $dbh->prepare($query);
    $params = array(
    ':amenity_id' => (int)$amenity,
    ':property_id' => (int)$property_id,
    );
    $stmt->execute($params);
    }
    $viewVars['success'] = true;
    }
    // Render the view
    render('add'),
    
  2. Create the view for the form at /path/to/webroot/properties/views/add.thtml:
    <?php if (!empty($viewVars['success'])): ?>
    <div class="information">Property saved successfully</div>
    <?php else: ?>
    <form action="add.php" method="post">
    <fieldset>
    <legend>Add Property</legend>
    <div class="input">
    <label>Type: </label>
    <input type="radio" name="type" value="1" /> Residential
    &nbsp;&nbsp;&nbsp;
    <input type="radio" name="type" value="2" />Commercial
    </div>
    <div class="input">
    <label>Transaction Type: </label>
    <input type="radio" name="transaction_type" value="1" /> Buy
    &nbsp;&nbsp;&nbsp;
    <input type="radio" name="transaction_type" value="2" />
    Rent
    &nbsp;&nbsp;&nbsp;
    <input type="radio" name="transaction_type" value="3" /> PG
    </div>
    <div class="input">
    <label>Description: </label>
    <textarea name="description" rows="5" cols="30"></textarea>
    </div>
    <div class="input">
    <label>Price ($): </label>
    <input type="text" name="price" size="5" />
    </div>
    <div class="input">
    <label>City: </label>
    <select name="city_id">
    <?php foreach ($viewVars['cities'] as $id => $name): ?>
    <option value="<?php echo $id; ?>">
    <?php echo $name; ?></option>
    <?php endforeach; ?>
    </select>
    </div>
    <div class="input">
    <label>Address: </label>
    <textarea name="address"></textarea>
    </div>
    <div class="input">
    <label>Zip Code: </label>
    <input type="text" name="zip_code" size="5" />
    </div>
    <div class="input">
    <label>Bedrooms: </label>
    <input type="text" name="bedrooms" size="1" />
    </div>
    <div class="input">
    <label>Bathrooms: </label>
    <input type="text" name="bathrooms" size="1" />
    </div>
    <div class="input">
    <label>Amenities: </label>
    <select name="amenities[]" size="5" multiple>
    <?php
    foreach ($viewVars['amenities'] as $id => $name): ?>
    <option value="<?php echo $id; ?>">
    <?php echo $name; ?>
    </option>
    <?php endforeach; ?>
    </select>
    </div>
    <div class="input">
    <label>Area: </label>
    <input type="text" name="area" size="4" />
    </div>
    <div class="input">
    <label>Built Year: </label>
    <select name="built_year">
    <option value="0">Under Construction</option>
    <?php
    $year = date('Y'),
    for ($i = $year; $i >= $year - 200; $i--) {
    ?>
    <option value="<?php echo $i; ?>">
    <?php echo $i; ?></option>
    <?php } ?>
    </select>
    </div>
    <div class="input">
    <label>Latitude: </label>
    <input type="text" name="latitude" size="3" />
    </div>
    <div class="input">
    <label>Longitude: </label>
    <input type="text" name="longitude" size="3" />
    </div>
    <div class="input">
    <label>&nbsp;</label>
    <input type="submit" name="submit" value="Add" />
    </div>
    </fieldset>
    </form>
    <?php endif; ?>
    
    
  3. Open add.php in a browser and add a property with some dummy data,as demonstrated in the following screenshot:
    Time for action - creating the form to add property

What just happened?

We created a PHP script, add.php, in our webroot to add properties. The PHP code is self explanatory.

Next, we created the view, in which we added the HTML form to input data for different fields related to a property.

Tip

Data validation

In our code we didn't add any sort of validations. In a real world application you should always add some validations to make sure that form data is not tainted and the field values are what we expected.

Now that we have the form to add a new property, go ahead and add a few dummy properties. For latitude and longitude values you might want to refer to Google maps http://maps.google.com.

Indexing the properties

The next step in our application is to index the data using Sphinx's indexer tool. We will be creating three separate forms for searching, but the same index will be used by all three search forms.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset