Connecting Tech Pros Worldwide Forums | Help | Site Map

Problems with Google-like Autosuggest

Newbie
 
Join Date: Dec 2007
Posts: 2
#1: Dec 8 '07
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('suggest.class.php');
// create a new Suggest instance
$suggest = new Suggest();
// retrieve the keyword passed as parameter
$keyword = $_GET['keyword'];
// clear the output
if(ob_get_length()) 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.php file
[PHP]
// load error handling module
require_once('error_handler.php');
// load configuration file
require_once('config.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($patterns, $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(MYSQLI_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]

Newbie
 
Join Date: Dec 2007
Posts: 2
#2: Dec 8 '07

re: Problems with Google-like Autosuggest


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

Thank you in advance.
Patrick
Attached Files
File Type: txt suggest.txt (13.3 KB, 61 views)
File Type: txt suggestpart2.txt (7.1 KB, 28 views)
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: Dec 10 '07

re: Problems with Google-like Autosuggest


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

Have you tried alerting the response from the Ajax request?
Reply