Time for action - setting up the application

  1. Create a properties directory in your webroot, /path/to/webroot/properties:
    $ mkdir /path/to/webroot/properties
    
  2. Create the /path/to/webroot/properties/views directory:
    $ mkdir /path/to/webroot/properties/views
    
  3. Copy the sphinxapi.php file from the Sphinx source directory to the properties directory:
    $ cp /path/to/sphinx-0.9.9/api/sphinxapi.php /path/to/webroot/properties/
    
  4. Create the file /path/to/webroot/properties/init.php with the following code:
    <?php
    /**
    * File: /path/to/webroot/properties/init.php
    */
    // Database connection credentials
    $dsn ='mysql:dbname=sphinx_properties;host=localhost';
    $user = 'root';
    $pass = '';
    // Instantiate the PDO (PHP 5 specific) class
    try {
    $dbh = new PDO($dsn, $user, $pass);
    } catch (PDOException $e){
    echo'Connection failed: '.$e->getMessage();
    }
    // Array to hold variables to be used in views
    $viewVars = array();
    /**
    * Method to fetch the contents of a view (thtml) file
    * and return the contents.
    * The html string returned by this method is then
    * placed in the master layout.
    *
    * @param string $view Name of the view file to be fetched.
    *
    * @return string HTML contents specific to the passed view
    */
    function get_view($view)
    {
    global $viewVars;
    // Start the output buffering so that the html output of the
    // view is not sent to the browser immediately.
    ob_start();
    // Include the view file which outputs the HTML
    include("views/$view.thtml");
    // Get the view contents in a variable i.e. whatever the
    // above view outputs, it gets stored in a variable
    $contents = ob_get_contents();
    // Clean the buffer
    ob_end_clean();
    return $contents;
    }//end get_view()
    /**
    * Method to render the page.
    * This method along with get_view()
    * acts as a very simple templating
    * engine and separates the view logic
    * from our php (business) logic.
    *
    * @param string $view Name of the view file to be rendered
    */
    function render($view)
    {
    $contents = get_view($view);
    include('views/layout.thtml'),
    }//end render()
    
  5. Create the master layout at /path/to/webroot/properties/views/layout.thtml:
    <!-- File: /path/to/webroot/properties/views/layout.thtml -->
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>Property portal</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
    <div id="header">
    <h1>Property search using Sphinx</h1>
    </div>
    <div id="nav">
    <ul>
    <li><a href="index.php">Home</a></li>
    <li><a href="search.php">Advanced Search</a></li>
    <li><a href="geo_search.php">Geolocation Search</a></li>
    <li><a href="add.php">Add property</a></li>
    </ul>
    </div>
    <div id="content">
    <?php echo $contents; ?>
    </div>
    </body>
    </html>
    
  6. Create /path/to/webroot/properties/style.css:
    /** File: /path/to/webroot/properties/style.css **/
    body {
    font-family: verdana,arial,sans-serif;
    background-color: #F7F5F2;
    font-size: 12px;
    margin: 0;
    overflow: auto;
    }
    #header {
    text-align: center;
    background-color: #606060;
    color: #ffffff;
    height: 70px;
    padding-top: 5px;
    }
    #nav ul {
    list-style: none;
    padding: 5px;
    margin: 0px;
    }
    #nav ul li {
    display: inline;
    padding: 5px 10px 5px 10px;
    border-right: 1px solid;
    }
    #nav {
    background-color: #000000;
    color: #ffffff;
    }
    #nav a {
    color: #ffffff;
    text-decoration: none;
    font-weight: bold;
    }
    #content {
    padding: 10px;
    }
    div.input {
    padding: 5px;
    }
    label {
    width: 110px;
    text-align: right;
    display: block;
    float: left;
    }
    .information {
    color: #28630B;
    }
    

What just happened?

As in the previous chapter, we created a common PHP file, init.php, which will be used to initialize database connection and contains a few other methods to render the output.

We also created an HTML layout and stylesheet to render the output as a nice looking web page.

Note

The files created in this exercise were not explained in great detail as they are similar to those created in Chapter 5, Feed Search. You should refer to this for further explanation.

At this point your directory structure will look like the following screenshot

What just happened?

Adding a property

The next step would be to create a form that will facilitate adding new properties. This form will have fields to specify property details; such as the type of property, city, amenities, and so on.

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

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