Problems with Google-like Autosuggest

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pjamrisk
    New Member
    • Dec 2007
    • 2

    Problems with Google-like Autosuggest

    Hi, I was wondering if one of the many guru's my help me out.
    Through several javascript books we have been trying to make an autocomplete feature for our department website for certain fields from a mysql database. The problem we are having is that once the autocomplete is triggered we only get back no results found instead of our query.

    I will post the code I currently have developed and see if someone can help us hunt this gost we have been having for over a month now.

    Thank you in advance.

    suggest.php file
    [PHP]
    // reference the file containing the Suggest class
    require_once('s uggest.class.ph p');
    // create a new Suggest instance
    $suggest = new Suggest();
    // retrieve the keyword passed as parameter
    $keyword = $_GET['keyword'];
    // clear the output
    if(ob_get_lengt h()) ob_clean();
    // headers are sent to prevent browsers from caching
    header('Expires : Mon, 26 Jul 1997 05:00:00 GMT' );
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
    header('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');
    header('Content-Type: text/xml');
    // send the results to the client
    echo $suggest->getSuggestions ($keyword);
    [/PHP]

    suggest.class.p hp file
    [PHP]
    // load error handling module
    require_once('e rror_handler.ph p');
    // load configuration file
    require_once('c onfig.php');

    // class supports server-side suggest & autocomplete functionality
    class Suggest
    {
    // database handler
    private $mMysqli;

    // constructor opens database connection
    function __construct()
    {
    // connect to the database
    $this->mMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD,
    DB_DATABASE);
    }

    // destructor, closes database connection
    function __destruct()
    {
    $this->mMysqli->close();
    }

    // returns all PHP functions that start with $keyword
    public function getSuggestions( $keyword)
    {
    // escape the keyword string
    $patterns = array('/\s+/', '/"+/', '/%+/');
    $replace = array('');
    $keyword = preg_replace($p atterns, $replace, $keyword);
    // build the SQL query that gets the matching functions from the database
    if($keyword != '')
    $query = 'SELECT name ' .
    'FROM suggest ' .
    'WHERE name LIKE "' . $keyword . '%"';
    // if the keyword is empty build a SQL query that will return no results
    else
    $query = 'SELECT name ' .

    'FROM suggest ' .
    'WHERE name=""';
    // execute the SQL query
    $result = $this->mMysqli->query($query );
    // build the XML response
    $output = '<?xml version="1.0" encoding="UTF-8" standalone="yes "?>';
    $output .= '<response>';
    // if we have results, loop through them and add them to the output
    if($result->num_rows)
    while ($row = $result->fetch_array(MY SQLI_ASSOC))
    $output .= '<name>' . $row['name'] . '</name>';
    // close the result stream
    $result->close();
    // add the final closing tag
    $output .= '</response>';
    // return the results
    return $output;
    }
    //end class Suggest
    }

    [/PHP]
  • pjamrisk
    New Member
    • Dec 2007
    • 2

    #2
    the javascript is quite large, so it is an attachment to this post.

    Thank you in advance.
    Patrick
    Attached Files

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Does the PHP work just as expected when you run it on its own?

      Have you tried alerting the response from the Ajax request?

      Comment

      Working...