0

Im trying to get this code to work and for the life of me can not get it going... I want a search that shows a Did you mean. with the code i have all i get it "Did you mean: Array l:6" what is wrong with what i have here?

$my_word = $_REQUEST['value'];
$bestMatch = array('word' => $my_word, 'match' => 2);
$result = mysql_query("SELECT keyword FROM athena");
while ($keyword = mysql_fetch_array($result)) {
    $lev = levenshtein ($keyword, $my_word, 1, 2, 1);
    if (!isset($lowest) || $lev < $lowest) {
       $bestMatch = array('word' => $keyword, 'match' => $lev);
       $lowest = $lev;
     }
 }
 if ($bestMatch['match'] > 0)
   echo 'Did you mean: <strong>'.$bestMatch['word'].'</strong> l:'.$bestMatch['match'];
David Morin
  • 485
  • 3
  • 5
  • 16
  • [`var_dump($bestMatch['word']);`](http://php.net/manual/en/function.var-dump.php) will tell you you are doing it wrong. – PeeHaa Jul 04 '12 at 14:06
  • 1
    Please, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://goo.gl/KJveJ). See the [**red box**](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is good PDO tutorial](http://goo.gl/vFWnC). – Madara's Ghost Jul 04 '12 at 15:32

4 Answers4

4

Your passing your entire search result set to the levenshtein() function instead of the keyword:

while ($row= mysql_fetch_array($result)) {
    $lev = levenshtein ($row['keyword'], $my_word, 1, 2, 1);
    if (!isset($lowest) || $lev < $lowest) {
       $bestMatch = array('word' => $row['keyword'], 'match' => $lev);
       $lowest = $lev;
    }
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • This did it... Now real quick could you explain what 1,2,1 references here...i assume it grabs the closest match. but what does each represent? – David Morin Jul 04 '12 at 14:13
  • They're unnecessary for what you're doing so you can safely remove them. – John Conde Jul 04 '12 at 14:15
  • man there has got to be a better way to do this... when i search for sched it says did you mean HOLD when it should say did you mean scheduling or similar – David Morin Jul 04 '12 at 14:20
  • http://php.net/manual/en/function.levenshtein.php So, your 1st "1" is what an addition cost, your "2" is what a replacement cost and your last "1" is what a deletion cost. Let's play with "bulb" and "suburb": you have to add "s" and "u" (so 1 + 1) and replace "l" by "r" (so 2), that make a levenshtein to 4. If you remove those params, your levenshtein would be 3 (2 adds and 1 replacement) – niconoe Jul 04 '12 at 14:33
1

$keyword is an array (one dimensional), not a single column. You should mysql_fetch_field the column you want

Oded
  • 145
  • 1
  • 7
0
<?php
while ($keyword = mysql_fetch_array($result))
?>

$keyword is an array! Your word is in $keyword[0] or $keyword['keyword']. replace your line 7 with

$bestMatch = array('word' => $keyword['keyword'], 'match' => $lev);
niconoe
  • 1,191
  • 1
  • 11
  • 25
0

As PeeHaa and jeroen already said, the MySQL result returns an array.

Dump it out and see what's in there

if ($bestMatch['match'] > 0) {
  var_dump($bestMatch['word']);
  echo 'Did you mean: <strong>'.$bestMatch['word'].'</strong> l:'.$bestMatch['match'];
}

Most likely there is another [ ] in there, so this might work:

echo 'Did you mean: <strong>'.$bestMatch['word'][0].'</strong> l:'.$bestMatch['match'];
Community
  • 1
  • 1
Peon
  • 7,902
  • 7
  • 59
  • 100