Link to home
Start Free TrialLog in
Avatar of verm1n
verm1n

asked on

How do I make this bsn.autosuggest script, from a javascript into a SQL working one?

Hello fellas,
Im trying to use a autosuggest searchscript on my site, and im currently interested in this one: http://www.brandspankingnew.net/archive/2006/08/ajax_auto-suggest_auto-complete.html --

But the thing is, that this is a javascript with some pre-written answers..

Instead of that, I want to use it in my SQL database, Anyone know how to do this?

The standard version from the site uses this methods, posted into the CODE below. I have no really clue where to being.

Further notice.. I really appreciate some help here!
File: test.php
 
<?php
 
/*
note:
this is just a static test version using a hard-coded countries array.
normally you would be populating the array out of a database
 
the returned xml has the following structure
<results>
	<rs>foo</rs>
	<rs>bar</rs>
</results>
*/
 
	$aUsers = array(
		"Adams, Egbert",
		"Altman, Alisha",
	);
	
	
	$aInfo = array(
		"Bedfordshire",
		"Buckinghamshire",
	);
	
	
	$input = strtolower( $_GET['input'] );
	$len = strlen($input);
	
	
	$aResults = array();
	
	if ($len)
	{
		for ($i=0;$i<count($aUsers);$i++)
		{
			// had to use utf_decode, here
			// not necessary if the results are coming from mysql
			//
			if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $input)
				$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
			
			//if (stripos(utf8_decode($aUsers[$i]), $input) !== false)
			//	$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
		}
	}
	
	header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
	header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
	header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header ("Pragma: no-cache"); // HTTP/1.0
	
	
	
	if (isset($_REQUEST['json']))
	{
		header("Content-Type: application/json");
	
		echo "{\"results\": [";
		$arr = array();
		for ($i=0;$i<count($aResults);$i++)
		{
			$arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}";
		}
		echo implode(", ", $arr);
		echo "]}";
	}
	else
	{
		header("Content-Type: text/xml");
 
		echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
		for ($i=0;$i<count($aResults);$i++)
		{
			echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>";
		}
		echo "</results>";
	}
?>
 
File: index.php - the main file with all design ect..
 
 
<script type="text/javascript">
	var options = {
		script:"test.php?json=true&limit=20&",
		varname:"input",
		json:true,
		shownoresults:false,
		maxresults:10,
		callback: function (obj) { document.getElementById('testid').value = obj.id; }
	};
	var as_json = new bsn.AutoSuggest('testinput', options);
	
	
	var options_xml = {
		script: function (input) { return "test.php?input="+input+"&testid="+document.getElementById('testid').value; },
		varname:"input"
	};
	var as_xml = new bsn.AutoSuggest('fram1', options_xml);
	var as_xml = new bsn.AutoSuggest('fram2', options_xml);
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of MatthiasVance
MatthiasVance
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of verm1n
verm1n

ASKER

Solved it! I now got a two boxes on my page with drop down meny function + its finding the results from a SQL database! Im so happy! Finally we can get some good grades on our projekt works,

=)
Would you please tell us how you solved this?

Kind regards,

Matthias Vance