Jump to content
salimalisalim

How to show the suggestion text according to prefix

Recommended Posts

Hi,
I have the below code to show the suggestion text, now it’s showing all the words which contain the letter when I first type in the text field. For example, If I typed the letter “a”, it should show only the words which start with the letter “a”, now it’s showing all the words which contain the letter “a” in the suggestion. Below is my code. Thanks in advance.

 


<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");

$x=$xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
  $hint="";
  for($i=0; $i<($x->length); $i++) {
    $y=$x->item($i)->getElementsByTagName('title');
    $z=$x->item($i)->getElementsByTagName('url');
    if ($y->item(0)->nodeType==1) {
      //find a link matching the search text
      if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
        if ($hint=="") {
          $hint="<a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        } else {
          $hint=$hint . "<br /><a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
  $response="no suggestion";
} else {
  $response=$hint;
}

//output the response
echo $response;
?> 

 

Share this post


Link to post
Share on other sites
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {

That is the bit to check if the string (the first argument) contains the desired letter/string (second argument). That's what you need to change. I suggest strncasecmp; the length to check should be the length of $q, not the ->nodeValue.

  • Like 1

Share this post


Link to post
Share on other sites
if (...) {

makes the decision about whether to include the item from the XML in the search results. The part inside it needs to be true.

stristr(..., ...)

What the stristr function is effectively doing is checking if the first argument (a string) contains the second argument (also a string). It doesn't technically return true (or false) like the if expects, but it's close enough that PHP allows it.

What you want is not "contains" but "starts with". Unfortunately PHP doesn't have a "starts with" function, but the strncasecmp I linked comes close: it takes two strings and compares the first few characters (you decide how many) to see if they match. The "n" means it compares a length you want, and "case" meaning it's case-insensitive (like the "i" in "stristr" means it's case-insensitive... yeah, one is "case" and one is "i", that's just the way it is). The function returns a number that measures how the two compare, and that number is 0 if they are the same.

Since you want "starts with", the length you need to compare is the length of the string you are searching for. "If the first (length of $q) characters are the same...". strlen is the function to get the length.

So you need to:
- Keep the overall if(...) structure
- Use strncasecmp instead of stristr, with the first two arguments being the two strings to compare (it doesn't actually matter which is which) and the third being the length to compare
- Use strlen with $q for the length
- Check that the strncasecmp function returns a number == 0

Give that a shot, and if you have problems then post what you came up with and a description of what's not working.

  • Like 1

Share this post


Link to post
Share on other sites

Thank you so much for the great descriptions, I modified my code as below, but still, it's giving me the previous result.

 

if (strncasecmp ($y->item(0)->childNodes->item(0)->nodeValue,$q, 10)) {

 

Share this post


Link to post
Share on other sites

1. I don't know where you came up with 10.
2. You need to check the return value from strncasecmp.

Take another look at my post.

  • Like 1

Share this post


Link to post
Share on other sites

I updated like below with the length of $q, but still, I am getting all the words containing the letter

if (strncasecmp ($y->item(0)->childNodes->item(0)->nodeValue,$q,strlen($q)))

 

Share this post


Link to post
Share on other sites

It's actually showing you all entries that do not begin with the letter.

3 hours ago, requinix said:

- Check that the strncasecmp function returns a number == 0

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.