Jump to content

AJAX AND PHP


etsted

Recommended Posts

$type = $_REQUEST['type'];        $sql = "SELECT tittel FROM $type GROUP BY titel";    $query = mysqli_query($con,$sql);    while($row = mysql_fetch_array($query))    {        $a[] = $row['titel'];    }        // get the q parameter from URL    $q=$_REQUEST["q"]; $hint="";        // lookup all hints from array if $q is different from ""     if ($q !== "")      { $q=strtolower($q); $len=strlen($q);        foreach($a as $name)        { if (stristr($q, substr($name,0,$len)))          { if ($hint==="")            { $hint=$name; }            else            { $hint .= "<br> $name"; }          }        }      }        // Output "no suggestion" if no hint were found    // or output the correct values     echo $hint==="" ? "no suggestions" : $hint;exit();

this script is supposed to show suggested titels according to what the user is typing in.

 

But if i have several titel in the DB like this:

"my new name"

"name"

"new"

and i were to search "my new name" then all of the above titels will show, when only the first should.

Link to comment
Share on other sites

That is because you are searching through 'haystack' ($q) for 'needle' of "my new name" or "name" or "new", which it will find 'my new name' and pass 'if' condition and add to $hint as $hint is empty, loop continues $hint not empty now so

go through 'else' condition $hint .= "<br> $name"; until end of loop.

 

You can either swap $q and $name around, or

if (strripos($q, $name) != -1)

or

if ($name == $q)

depending on what you require

Link to comment
Share on other sites

ive tried to update it, and it almost work as i want i to but there are some small problems.

    if ($q !== "")      { $q=strtolower($q); $len=strlen($q);        foreach($a as $name)        {            if (strcmp(substr($name,$len), substr($q,0,$len)))          {               if ($hint==="")                { $hint=$name; }              else                { $hint .= "<br> $name"; }          }        }      }   

this script will shows you only what you search for, but if you search for "n"

and i have:

"green"

"nice tv"

then both of them will show, basically i want to make sure that when someone search for something the only matches should have what the user searched for at the beginning of the name, Not in the middle or the end or something like that.

Link to comment
Share on other sites

shoudnt this work?


if ($q !== "")
{ $q=strtolower($q); $len=strlen($q);
foreach($a as $name)
{
if ( (strcmp(substr($name,0,$len), substr($q,0,$len)) ) == true)
{
if ($hint==="")
{ $hint=$name; }
else
{ $hint .= "<br> $name"; }
}
}
}

Because it dont

 

In the DB i have 4 titels "6", "d", "green" and "nature" and if i type "n" then 6,d and green shows up then i try to type in "nature " (notice the space i put after nature) and then nature shows up at the end

Edited by etsted
Link to comment
Share on other sites

then both of them will show, basically i want to make sure that when someone search for something the only matches should have what the user searched for at the beginning of the name, Not in the middle or the end or something like that.

 

 

because i thought that is what you wanted by this quote in red, if you place at beginning as well it will find your search criteria anywhere within text string.

Link to comment
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
×
×
  • Create New...