Nieuw 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:
<?php
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'root' ,'usbw', 'links');
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['search'])) {
$search = $db->real_escape_string($_POST['search']);
// Is the string length greater than 0?
if(strlen($search) >0) {
// Run the query: We use LIKE '$search%'
// The percentage sign is a wild-card, in my example of countries it works like this...
// $search = 'Uni';
// Returned data = 'United States, United Kindom';
// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
// eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$search%' LIMIT 10
$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");
if($query) {
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
while ($result = $query ->fetch_object()) {
// Format the results, im using <li> for the list, you can change it.
// The onClick function fills the textbox with the result.
$all = $result->score;
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill(\''.$all.'\');">'.$all.'</li>';
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
// Dont do anything.
} // There is a search.
} else {
echo 'There should be no direct access to this script!';
}
}
?>
<?php // PHP5 Implementation - uses MySQLi. // mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase'); $db = new mysqli('localhost', 'root' ,'usbw', 'links'); if(!$db) { // Show error if we cannot connect. echo 'ERROR: Could not connect to the database.'; } else { // Is there a posted query string? if(isset($_POST['search'])) { $search = $db->real_escape_string($_POST['search']); // Is the string length greater than 0? // Run the query: We use LIKE '$search%' // The percentage sign is a wild-card, in my example of countries it works like this... // $search = 'Uni'; // Returned data = 'United States, United Kindom'; // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE. // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$search%' LIMIT 10 $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"); if($query) { // While there are results loop through them - fetching an Object (i like PHP5 btw!). while ($result = $query ->fetch_object()) { // Format the results, im using <li> for the list, you can change it. // The onClick function fills the textbox with the result. $all = $result->score; // YOU MUST CHANGE: $result->value to $result->your_colum echo '<li onClick="fill(\''.$all.'\ ');">'.$all.'</li>'; } } else { echo 'ERROR: There was a problem with the query.'; } } else { // Dont do anything. } // There is a search. } else { echo 'There should be no direct access to this script!'; } } ?>
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
|