login  Naam:   Wachtwoord: 
Registreer je!
 Forum

AutoComplete functie

Offline tuxy - 12/12/2010 14:58 (laatste wijziging 12/12/2010 15:02)
Avatar van tuxyNieuw lid Hallo,

Ben momenteel aan het experimenteren met een autosuggest/autocomplete functie die ik van het net heb gehaald:
AutoCompleter

Heb deze aangepast voor mijn eigen zoekrobot.
Deze werkt echter wel, maar probleem is dat, wanneer er tekens worden ingevoerd, deze de volledige teksten toont vd gevonden resultaten in de suggestBox wat uiteraard niet de bedoeling is.

Wat ik eigenlijk zou willen is dat deze enkel de gevonden woorden netjes onder elkaar weergeeft, dit zoals de autoSuggest-functie van google.
Dus is de bedoeling dat gevonden woorden uit de inhoud wordt gefilterd en worden gepresenteerd in de suggestBox.

Hieronder zie je de code waarmee ik aan het experimenteren ben:
  1. <?php
  2.  
  3. // PHP5 Implementation - uses MySQLi.
  4. // mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
  5. $db = new mysqli('localhost', 'root' ,'usbw', 'links');
  6.  
  7. if(!$db) {
  8. // Show error if we cannot connect.
  9. echo 'ERROR: Could not connect to the database.';
  10. } else {
  11. // Is there a posted query string?
  12. if(isset($_POST['search'])) {
  13. $search = $db->real_escape_string($_POST['search']);
  14.  
  15. // Is the string length greater than 0?
  16. if(strlen($search) >0) {
  17. // Run the query: We use LIKE '$search%'
  18. // The percentage sign is a wild-card, in my example of countries it works like this...
  19. // $search = 'Uni';
  20. // Returned data = 'United States, United Kindom';
  21.  
  22. // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
  23. // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$search%' LIMIT 10
  24. $query = $db->query("SELECT CONCAT (title, description, keywords) AS score FROM links WHERE title LIKE '%$search%' OR description LIKE '%$search%' OR keywords LIKE '%$search%' AND (end_date IS NULL OR end_date > CURRENT_DATE( ) OR end_date = '0000-00-00') LIMIT 10");
  25. if($query) {
  26. // While there are results loop through them - fetching an Object (i like PHP5 btw!).
  27. while ($result = $query ->fetch_object()) {
  28. // Format the results, im using <li> for the list, you can change it.
  29. // The onClick function fills the textbox with the result.
  30.  
  31. $all = $result->score;
  32. // YOU MUST CHANGE: $result->value to $result->your_colum
  33. echo '<li onClick="fill(\''.$all.'\');">'.$all.'</li>';
  34. }
  35. } else {
  36. echo 'ERROR: There was a problem with the query.';
  37. }
  38. } else {
  39. // Dont do anything.
  40. } // There is a search.
  41. } else {
  42. echo 'There should be no direct access to this script!';
  43. }
  44. }
  45. ?>


Hopelijk kan iemand mij helpen, want heb al gegoogled naar andere tutorials over autoSuggest, maar daarin wordt niet uitgelegd hoe woorden eruit te filteren.

Grtz Christophe

0 antwoorden

Gesponsorde links
Er zijn nog geen reacties op dit bericht.
Je moet ingelogd zijn om een reactie te kunnen posten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.202s