Jump to content

Autosuggest Implementation - Hardcoded array > mysql results help needed.


ineedhelpbigtime

Recommended Posts

Hello again, i am struggling to modify a nifty auto suggest function. It came hardcoded with an array, and i need to modify it to work form my DB. Could someone have a look for me?

 

The code i am using is from http://www.brandspankingnew.net/specials/ajax_autosuggest/ajax_autosuggest_autocomplete.html.

 

My cut down version of the php is as follows:

 

<?php
include(inc/connect.php);

// assign search input and set limit.

$input = strtolower( $_GET['input'] );
$len = strlen($input);
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;

// results

$getName_sql	=	"SELECT username FROM users WHERE username LIKE '%$input%' OR names LIKE '%$input%'";
$getName		=	mysql_query($getName_sql);


// I have tried a number of things to convert the output of the above query into something that the script can handle, but probably incorrect methods...

$aUsers = array(
	"Ädams, Egbert",
	"Altman, Alisha",
	"Luke, Stagg"
);

$aResults = array();
$count = 0;


// If stringlength...

if ($len)
{
	for ($i=0;$i<count($aUsers);$i++)   // counts...
	{
		// 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)
		{
			$count++;
			$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]) );
		}

		if ($limit && $count==$limit)
			break;
	}
}

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 "]}";
}

?>

 

I have added my search query but i am unable to output the query results in a format that the rest of the code will accept (and i'm assuming the rest of the code will accept it...). The code above currently is still working from the 3 names specified in the manual array.

 

As always your help is greatly appreciated.

I have now been given the code to make the php work... but i am getting one weird error in firefox error console:

 

Warning: Error in parsing value for property 'background-image'.  Declaration dropped.
Source File: http://localhost/autosuggest_v2.1.3/css/autosuggest_inquisitor.css
Line: 137

 

This is the only error, which leads me to believe the SQL is now working... But the autosuggest is not. Any suggestions? I desperately need to get the auto suggest working, if anyone has any easier tutorials on how to implement autosuggest please let me know.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.