etsted Posted September 7, 2014 Share Posted September 7, 2014 $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 More sharing options...
davej Posted September 7, 2014 Share Posted September 7, 2014 Where do you define the $con variable? Link to comment Share on other sites More sharing options...
etsted Posted September 7, 2014 Author Share Posted September 7, 2014 why does that matter? its actually right above where i call it like include_once("connect.php"); Link to comment Share on other sites More sharing options...
dsonesuk Posted September 7, 2014 Share Posted September 7, 2014 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 More sharing options...
etsted Posted September 7, 2014 Author Share Posted September 7, 2014 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 More sharing options...
etsted Posted September 8, 2014 Author Share Posted September 8, 2014 (edited) 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 September 8, 2014 by etsted Link to comment Share on other sites More sharing options...
etsted Posted September 8, 2014 Author Share Posted September 8, 2014 i noticed something else, it doesnt show titel that does have a match. Link to comment Share on other sites More sharing options...
dsonesuk Posted September 8, 2014 Share Posted September 8, 2014 Well you could filter directly from sql query, to show title that start with, then filter it further. SELECT tittel FROM $type WHERE title LIKE '$q%' GROUP BY titel Link to comment Share on other sites More sharing options...
etsted Posted September 8, 2014 Author Share Posted September 8, 2014 why dont i put the % at the front? Link to comment Share on other sites More sharing options...
dsonesuk Posted September 8, 2014 Share Posted September 8, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now