Jump to content
albridge

need a bit of clarification

Recommended Posts

saw this example on the w3school site on creating an auto suggestion ajax/php form. it works perfect but i don't totally understand how it all works. would appreciate anyone who can do a bit more commenting on the code so i can get how it really works.the complete tutorial is found here the complete code is herei especially need clarification in the following part of the code especially the areas with $hint. just dont get that part

<?php//lookup all hints from array if length of q>0if (strlen($q) > 0)  {  $hint="";  for($i=0; $i<count($a); $i++)	{	if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))	  {	  if ($hint=="")		{		$hint=$a[$i];		}	  else		{		$hint=$hint." , ".$a[$i];		}	  }	}  }// Set output to "no suggestion" if no hint were found// or to the correct valuesif ($hint == "")  {  $response="no suggestion";  }else  {  $response=$hint;  }//output the responseecho $response;?>

Share this post


Link to post
Share on other sites

The array $a holds a list of words that can be used to provide an auto complete hint. The first loops works through each character of the query string $q and compares it to the elements in $a. If the two match, then $hint is set to the full entry in the suggestion array $a. As $q gets longer, the comparison can be made more accurately, so it will appear to refine the $hint. That might not have been the clearest explanation, but I hope it helped somewhat. Perhaps an example.If I type 'a', then $q == 'a'.The loop finds an entry from $a that starts with 'a' - let's say aardvark - and returns the hint $hint == 'aardvark'.Then I type 'ab', so the loops finds any entries starting with 'ab' and returns that - say 'abba'.Next I type 'aby' and the only word in the dictionary is 'abyss', so that is what $hint becomes. It's a constant refinement until the only viable option becomes apparent.

Share this post


Link to post
Share on other sites
The array $a holds a list of words that can be used to provide an auto complete hint. The first loops works through each character of the query string $q and compares it to the elements in $a. If the two match, then $hint is set to the full entry in the suggestion array $a. As $q gets longer, the comparison can be made more accurately, so it will appear to refine the $hint. That might not have been the clearest explanation, but I hope it helped somewhat. Perhaps an example.If I type 'a', then $q == 'a'.The loop finds an entry from $a that starts with 'a' - let's say aardvark - and returns the hint $hint == 'aardvark'.Then I type 'ab', so the loops finds any entries starting with 'ab' and returns that - say 'abba'.Next I type 'aby' and the only word in the dictionary is 'abyss', so that is what $hint becomes. It's a constant refinement until the only viable option becomes apparent.
first of all i really appreciate you taking time to try to make things clear to me. means a lot to me. however i would like to point out some other specific areas i want cleared out. first please could explain this part of the code
if ($hint=="")		{		$hint=$a[$i];

that piece of code looks like if $hint is same as an empty string then set it to an element of the array $a. i dont get that cause i think if $hint is empty then it should be empty. how come a value is assigned when $hint is empty.then i would like to get some clarification on the else part of the condition also

}	  else		{		$hint=$hint." , ".$a[$i];

especially the part of $hint=$hint.","$a[$i]; thanks in for your help here.

Share this post


Link to post
Share on other sites

You need to look at the context that code is in. With this entire piece:

if (strlen($q) > 0)  {  $hint="";  for($i=0; $i<count($a); $i++)	{	if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))	  {	  if ($hint=="")		{		$hint=$a[$i];		}	  else		{		$hint=$hint." , ".$a[$i];		}	  }	}  }

That starts $hint off as an empty string. The loop is adding each item in the array to the $hint string if it matches what is being looked for. The if statement that is checking if $hint is empty will only be true the first time through the loop. So, if $hint is empty then just set to the first matching element. If $hint is not empty, then add a comma then the next element. That's basically just making sure that $hint is a comma-separated list of matches. There are other ways to do the same thing.

Share this post


Link to post
Share on other sites
The array $a holds a list of words that can be used to provide an auto complete hint. The first loops works through each character of the query string $q and compares it to the elements in $a. If the two match, then $hint is set to the full entry in the suggestion array $a. As $q gets longer, the comparison can be made more accurately, so it will appear to refine the $hint. That might not have been the clearest explanation, but I hope it helped somewhat. Perhaps an example.If I type 'a', then $q == 'a'.The loop finds an entry from $a that starts with 'a' - let's say aardvark - and returns the hint $hint == 'aardvark'.Then I type 'ab', so the loops finds any entries starting with 'ab' and returns that - say 'abba'.Next I type 'aby' and the only word in the dictionary is 'abyss', so that is what $hint becomes. It's a constant refinement until the only viable option becomes apparent.
just want to say thanks again for your help. took another look at the code and got to figure out the confusing areas. thanks again for all your help. thanks chibineku and justsomeguy for your help. really appreciate your contribution.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...