AutoSuggest problemen
Hallo,
Ben momenteel bezig aan het experimenteren met een 'autosuggest'-functie, die ik van het net heb gehaald.
AutoSuggest
Het is de bedoeling dat deze verschillende velden doorzoekt, nl. title, description, keywords. Daarvoor maak ik gebruik van fulltext search.
- Het eerste probleem is echter als er 3 tekens ingevoerd worden, de volledige inhoud vh gevonden veld wordt getoond ipv enkel het gevonden woord.
Hoe dien ik deze regel 'echo '<li onClick="fill(\''.$result->description.'\');">'.$result->description.'</li>';' in te stellen, zodanig deze ook de woorden toont uit de title of keywords? Ik veronderstel dat 'AS naam' dient toegevoegd te worden aan de query en dan ook '->description' dien te wijzigen in '->naam'?
Hieronder de code die wordt aangeroepen, wanneer het zoekveld wordt ingevuld.
Christophe
Ben momenteel bezig aan het experimenteren met een 'autosuggest'-functie, die ik van het net heb gehaald.
AutoSuggest
Het is de bedoeling dat deze verschillende velden doorzoekt, nl. title, description, keywords. Daarvoor maak ik gebruik van fulltext search.
- Het eerste probleem is echter als er 3 tekens ingevoerd worden, de volledige inhoud vh gevonden veld wordt getoond ipv enkel het gevonden woord.
Hoe dien ik deze regel 'echo '<li onClick="fill(\''.$result->description.'\');">'.$result->description.'</li>';' in te stellen, zodanig deze ook de woorden toont uit de title of keywords? Ik veronderstel dat 'AS naam' dient toegevoegd te worden aan de query en dan ook '->description' dien te wijzigen in '->naam'?
Hieronder de code die wordt aangeroepen, wanneer het zoekveld wordt ingevuld.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'root' ,'usbw', 'blinks');
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
$search = $search.'*';
$query = $db->query("SELECT * FROM links WHERE MATCH(title, description, keywords) AGAINST ('".$search."' IN BOOLEAN MODE) AND (end_date IS NULL OR end_date > CURRENT_DATE( ) OR end_date = 0000-00-00)");
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.
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill(\''.$result->description.'');">'.$result->description.'</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!';
}
}
?>
// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'root' ,'usbw', 'blinks');
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
$search = $search.'*';
$query = $db->query("SELECT * FROM links WHERE MATCH(title, description, keywords) AGAINST ('".$search."' IN BOOLEAN MODE) AND (end_date IS NULL OR end_date > CURRENT_DATE( ) OR end_date = 0000-00-00)");
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.
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill(\''.$result->description.'');">'.$result->description.'</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!';
}
}
?>
Christophe
Gewijzigd op 11/12/2010 11:41:05 door Christophe Hollebeke
Zoals je aan de kleurtjes kan zien staat het al niet goed.
Code (php)
1
2
2
<?php
echo '<li onClick="fill(\''.$result->description.'\');">'.$result->description.'</li>';
echo '<li onClick="fill(\''.$result->description.'\');">'.$result->description.'</li>';
Gewijzigd op 11/12/2010 11:44:17 door Niels K
Niels,
Voorlopig heb ik dit nog niet bekeken, maar het functioneert wel.
Maar toch bedankt voor je terechte opmerking.
Aangepast:
Voorlopig heb ik dit nog niet bekeken, maar het functioneert wel.
Maar toch bedankt voor je terechte opmerking.
Aangepast:
Gewijzigd op 11/12/2010 12:46:37 door Christophe Hollebeke




