• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

Need help translating php code into either JavaScript or JSP code

Dear fellow developers:

I have a php file that is part of an Ajax script that I need to use as part of a JSP application.  This Ajax application is an autocomplete/autosuggest script, and I am wondering if it is at all possible for the php code that is contained in the test.php file, to be converted into something more "neutral" like JavaScript, or Java/JSP code?  Can this be done?  While I am aware that there are other autocomplete scripts that I can use for my application, I really like this one in particular, as it is visually very nice, and would like very much to incorporate it in my application.

The autocomplete script can be found here:

http://www.brandspankingnew.net/specials/ajax_autosuggest/ajax_autosuggest_autocomplete.html

and the file that I am referring to can be found below.

Thanks to all who reply.
Sincerely;
Fayyaz
<?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(
		"Ãdams, Egbert",
		"Altman, Alisha",
		"Archibald, Janna",
		"Auman, Cody",
		"Bagley, Sheree",
		"Ballou, Wilmot",
		"Bard, Cassian",
		"Bash, Latanya",
		"Beail, May",
		"Black, Lux",
		"Bloise, India",
		"Blyant, Nora",
		"Bollinger, Carter",
		"Burns, Jaycob",
		"Carden, Preston",
		"Carter, Merrilyn",
		"Christner, Addie",
		"Churchill, Mirabelle",
		"Conkle, Erin",
		"Countryman, Abner",
		"Courtney, Edgar",
		"Cowher, Antony",
		"Craig, Charlie",
		"Cram, Zacharias",
		"Cressman, Ted",
		"Crissman, Annie",
		"Davis, Palmer",
		"Downing, Casimir",
		"Earl, Missie",
		"Eckert, Janele",
		"Eisenman, Briar",
		"Fitzgerald, Love",
		"Fleming, Sidney",
		"Fuchs, Bridger",
		"Fulton, Rosalynne",
		"Fye, Webster",
		"Geyer, Rylan",
		"Greene, Charis",
		"Greif, Jem",
		"Guest, Sarahjeanne",
		"Harper, Phyllida",
		"Hildyard, Erskine",
		"Hoenshell, Eulalia",
		"Isaman, Lalo",
		"James, Diamond",
		"Jenkins, Merrill",
		"Jube, Bennett",
		"Kava, Marianne",
		"Kern, Linda",
		"Klockman, Jenifer",
		"Lacon, Quincy",
		"Laurenzi, Leland",
		"Leichter, Jeane",
		"Leslie, Kerrie",
		"Lester, Noah",
		"Llora, Roxana",
		"Lombardi, Polly",
		"Lowstetter, Louisa",
		"Mays, Emery",
		"Mccullough, Bernadine",
		"Mckinnon, Kristie",
		"Meyers, Hector",
		"Monahan, Penelope",
		"Mull, Kaelea",
		"Newbiggin, Osmond",
		"Nickolson, Alfreda",
		"Pawle, Jacki",
		"Paynter, Nerissa",
		"Pinney, Wilkie",
		"Pratt, Ricky",
		"Putnam, Stephanie",
		"Ream, Terrence",
		"Rumbaugh, Noelle",
		"Ryals, Titania",
		"Saylor, Lenora",
		"Schofield, Denice",
		"Schuck, John",
		"Scott, Clover",
		"Smith, Estella",
		"Smothers, Matthew",
		"Stainforth, Maurene",
		"Stephenson, Phillipa",
		"Stewart, Hyram",
		"Stough, Gussie",
		"Strickland, Temple",
		"Sullivan, Gertie",
		"Swink, Stefanie",
		"Tavoularis, Terance",
		"Taylor, Kizzy",
		"Thigpen, Alwyn",
		"Treeby, Jim",
		"Trevithick, Jayme",
		"Waldron, Ashley",
		"Wheeler, Bysshe",
		"Whishaw, Dodie",
		"Whitehead, Jericho",
		"Wilks, Debby",
		"Wire, Tallulah",
		"Woodworth, Alexandria",
		"Zaun, Jillie"
	);
	
	
	$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);
	$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
	
	
	$aResults = array();
	$count = 0;
	
	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)
			{
				$count++;
				$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
			}
			
			if ($limit && $count==$limit)
				break;
		}
	}
	
	
	
	
	
	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>";
	}
?>

Open in new window

0
fsyed
Asked:
fsyed
  • 3
  • 3
2 Solutions
 
absxCommented:
Hi,

The search results are gonna have to be generated on the server-end, ruling out a pure Javascript solution. The most common solution is, as above, to use a PHP/JSP/ASP page feeding the results in a standard form (XML/Json in the above example). I wouldn't say JSP would be more neutral than PHP, but it is possible to convert this script to any server-side web application platform as long as it's able to output HTTP headers and the results in the correct format:

<results>
<rs id="41" info="Kent">Harper, Phyllida</rs>
<rs id="42" info="Lancashire">Hildyard, Erskine</rs>
<rs id="43" info="Leicestershire">Hoenshell, Eulalia</rs>
</results>
0
 
fsyedAuthor Commented:
Dear Absx:

I agree with what you're saying, my issue is that I unfortunately don't know php, and was hoping to have someone translate the php code to jsp.  If this is not possible, at least in pseudocode, identify in the above file what it is doing, so that I can go ahead and code an equivalent file in JSP.

Thanks for your comment!

Sincerely;
Fayyaz
0
 
absxCommented:
Hi fsyed,

I'm afraid I never worked with JSP, so I'm not gonna be too helpful. Here's a quick draft of what's going on in pseudocode, though.

// Assign users to an array (or fetch from DB)
users(1) = "John"
users(2) = "Mary"
users(3) = "Sue"
 
// Get user's input parameters "input" (and possibly "limit") from HTTP request variables
searchterm = (get HTTP request parameter( "input" ))
if ( request parameter "limit" exists ) {
	limit = (get HTTP request parameter( "limit" ))
}
 
 
// Gather matching users and locations to result array
results = array
j = 0
if ( (length of searchterm) > 1 ) {
 
	// Scan through the users array for matches
	for i from 0 to count of users {
	
		if( value of users at i contains searchterm ) {
			//add value of users at i to results array
			results(j) = array(i, users(i))
			j = j + 1
		}
	}
}
 
// Output results in XML
output HTTP header "Content-Type: text/xml"
write "<?xml version='1.0' encoding='utf-8' ?><results>"
for i from 0 to count of results {
	write "<rs id='" + results(i)(0) + "'> " + results(i)(1) + "</rs>"
}
write "</results>"
		

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fsyedAuthor Commented:
Dear absx:

Thanks so much for your pseudocode.  Based on it, I think I can come up with an equivalent JSP version.  For the sake of completion, is it possible for you to provide the pseudocode for the JSON version as well?

Thanks again for your effort.

Sincerely;
Fayyaz
0
 
absxCommented:
Hi,

Here.
// Assign users to an array (or fetch from DB)
users(1) = "John"
users(2) = "Mary"
users(3) = "Sue"
 
// Get user's input parameters "input" (and possibly "limit") from HTTP request variables
searchterm = (get HTTP request parameter( "input" ))
if ( request parameter "limit" exists ) {
	limit = (get HTTP request parameter( "limit" ))
}
 
 
// Gather matching users and locations to result array
results = array
j = 0
if ( (length of searchterm) > 1 ) {
 
	// Scan through the users array for matches
	for i from 0 to count of users {
	
		if( value of users at i contains searchterm ) {
			//add value of users at i to results array
			results(j) = array(i, users(i))
			j = j + 1
		}
	}
}
 
if(HTTP request parameter( "json" ) exists) {
	// Output results in JSON
	output HTTP header "Content-Type: application/json"
	write "{'results': ["
	for i from 0 to count of results {
		write "{'id': '" + results(i)(0) + "', 'value': '" + results(i)(1) + "', 'info': ''}"
		
		// Separate with comma if not last result
		if(i != count of results)
			write ","
	}
	write "]}"
	
}
 
else if ( HTTP request paramenter( "xml" ) exists) {
	// Output results in XML
	output HTTP header "Content-Type: text/xml"
	write "<?xml version='1.0' encoding='utf-8' ?><results>"
	for i from 0 to count of results {
		write "<rs id='" + results(i)(0) + "'> " + results(i)(1) + "</rs>"
	}
	write "</results>"
}		

Open in new window

0
 
fsyedAuthor Commented:
Thanks so much absx for your patience.  With your help, I was able to develop a servlet (I realized I didn't need a JSP), that did exactly what the above php script was doing, and now my autocomplete script is working as I had hoped.  Thanks so much for your help!

Sincerely;
Fayyaz
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now