Jump to content

Help with Autosuggest code


Fenhopi

Recommended Posts

Hi, I have this code to suggest names from my database to people searching. However, how would I add more info to the result it displays then just firstname and lastname. Like, if i wanted username and email to be displayed under the name?

 

Code:

<?php

include("include/session.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>
*/

$GetNames = "SELECT firstname, lastname, username, country FROM users";
$GetNamesConnect = $database->query($GetNames);

While($row = mysql_fetch_array($GetNamesConnect))
{
   $aUsers[] = $row['firstname'] . " " . $row['lastname'] . ", ";
}


$aInfo = array(
	"Bedfordshire",
	"Buckinghamshire",
	"Cambridgeshire",
	"Cheshire",
	"Cornwall",
	"Cumbria",
	"Derbyshire",
	"Devon", 
	"Dorset", 
	"Durham", 
	"East Sussex",
	"Essex",
	"Gloucestershire",
	"Hampshire",
	"Hertfordshire", 
	"Kent", 
	"Lancashire",
	"Leicestershire",
	"Lincolnshire",
	"Norfolk",
	"Northamptonshire",
	"Northumberland", 
	"North Yorkshire", 
	"Nottinghamshire",
	"Oxfordshire",
	"Shropshire",
	"Somerset",
	"Staffordshire",
	"Suffolk", 
	"Surrey",
	"Warwickshire",
	"West Sussex",
	"Wiltshire",
	"Worcestershire", 
	"Durham", 
	"East Sussex",
	"Essex",
	"Gloucestershire",
	"Hampshire",
	"Hertfordshire", 
	"Kent", 
	"Lancashire",
	"Leicestershire",
	"Lincolnshire",
	"Norfolk",
	"Northamptonshire",
	"Northumberland", 
	"North Yorkshire", 
	"Nottinghamshire",
	"Oxfordshire",
	"Shropshire",
	"Somerset",
	"Staffordshire",
	"Suffolk", 
	"Surrey",
	"Warwickshire",
	"West Sussex",
	"Wiltshire",
	"Worcestershire", 
	"Durham", 
	"East Sussex",
	"Essex",
	"Gloucestershire",
	"Hampshire",
	"Hertfordshire", 
	"Kent", 
	"Lancashire",
	"Leicestershire",
	"Lincolnshire",
	"Norfolk",
	"Northamptonshire",
	"Northumberland", 
	"North Yorkshire", 
	"Nottinghamshire",
	"Oxfordshire",
	"Shropshire",
	"Somerset",
	"Staffordshire",
	"Suffolk", 
	"Surrey",
	"Warwickshire",
	"West Sussex",
	"Wiltshire",
	"Worcestershire", 
	"Durham", 
	"East Sussex",
	"Essex",
	"Gloucestershire",
	"Hampshire",
	"Hertfordshire", 
	"Kent", 
	"Lancashire",
	"Leicestershire",
	"Lincolnshire",
	"Norfolk",
	"Northamptonshire",
	"Northumberland", 
	"North Yorkshire", 
	"Nottinghamshire"
);


$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>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/220909-help-with-autosuggest-code/
Share on other sites

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.