zoek-suggestie

Gesponsorde koppelingen

PHP script bestanden

  1. zoek-suggestie

« Lees de omschrijving en reacties

//searchSuggestion.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

/**
 * @author Nico Kaag
 * @copyright 2009
 */

error_reporting(E_ALL);

/**
 * Give a list of search suggestions for a speciffic key word
 * @param string $keyword The keyword to search for
 * @param string $table The table in wich to search
 * @param string $field The field in wich to search
 * @param int $maxDistance The maximum difference between the keyword and suggestion
**/

function searchSuggestion($keyword, $table, $field, $maxDistance = 5)
{

    //Variable to store the closest match in
    //distance is -1 because it can't be an exact match, and it can be a possitive match

    $closestMatch = array('tag' => '', 'distance' => '-1');
    $return = array();
    $testKeyword = strtolower($keyword);
    $query = "SELECT ". $field ." FROM ". $table;
    $result = mysql_query($query);
    if (!$result || mysql_num_rows($result) == 0)
    {

        return false;
    }

    
    else
    {
        while ($row = mysql_fetch_assoc($result))
        {

            $tag = $row[$field];
            $testTag = strtolower($row[$field]);
            // calculate the distance between the input word,
            // and the current word

            $levenstheinDistance = levenshtein($testKeyword, $testTag, 1, 2, 1);
            //$levenstheinRatio = max(array(strlen($testTag), strlen($testKeyword))) / $levenstheinDistance;
        
            // if this distance is less than the next found shortest
            // distance, OR if a next shortest word has not yet been found

            if ($levenstheinDistance <= $closestMatch['distance'] || $closestMatch['distance'] < 0 && $levenstheinDistance <= $maxDistance) {
                // set the closest match, and shortest distance
                $closestMatch['tag']  = $tag;
                $closestMatch['distance'] = $levenstheinDistance;
            }
        }

        
        //If a tag is found, return the array with the closest match
        if (!empty($closestMatch['tag']))
        {

            return $closestMatch['tag'];
        }

        
        //If no match is found, return false
        else
        {
            return false;
        }
    }
}


?>


//index.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php

/**
 * @author Nico Kaag
 * @copyright 2009
 */

error_reporting(E_ALL);

include 'config.php';
include 'searchSuggestion.php';
?>


<form method="post" action="index.php">
Zoek: <input type="text" name="searchWord"><br />
<input type="submit" value="Zoeken!">
</form>

<br /><br />
<?php
if (isset($_POST['searchWord']))
{

    $keyword = mysql_real_escape_string($_POST['searchWord']);
    $query = "SELECT tag FROM tags WHERE tag = '". $keyword ."' LIMIT 0,1";
    $result = mysql_query($query);
    if (mysql_num_rows($result) == 1)
    {

        $row = mysql_fetch_assoc($result);
        echo 'Ik vond: '. $row['tag'];
    }

    else
    {
        $closestMatch = searchSuggestion($keyword, "tags", "tag");
        
        if (!$closestMatch)
        {

            echo 'Ik kon helaas niks voor u vinden.';
        }

        else
        {
            echo 'Zoekt u soms: <b>'. $closestMatch .'</b>?';
        }
    }
}

[
/code]

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.