Riaz
Riaz

Reputation: 11

Auto completion with multiple unordored words

I'm using this script and I want to modify/update it to work in this way:

In my database I have for example: hello world, my name is John. When I search Hello World or orld, m or name i I want to find the item, but if I search world hello or name John it doesn't work. So, I'm looking for the way to make it work. Could someone help me ?

Many Thanks

index.php

<script type="text/javascript">
    $(function(){
        $(".search").keyup(function() { 
            var searchid = $(this).val();
            var dataString = 'search=' + searchid;
            if (searchid != '') {
                $.ajax({
                    type: "POST",
                    url: "search.php",
                    data: dataString,
                    cache: false,
                    success: function(html) {
                        $("#result").html(html).show();
                    }
                });
            }
            return false;    
        });

        jQuery("#result").live("click", function(e){ 
            var $clicked = $(e.target);
            var $name = $clicked.find('.name').html();
            var decoded = $("<div/>").html($name).text();
            $('#searchid').val(decoded);
        });

        jQuery(document).live("click", function(e) { 
            var $clicked = $(e.target);
            if (! $clicked.hasClass("search")){
                jQuery("#result").fadeOut(); 
            }
        });

        $('#searchid').click(function(){
            jQuery("#result").fadeIn();
        });
    });
</script>
<style type="text/css">
    body { 
        font-family: Tahoma, Geneva, sans-serif;
        font-size: 15px;
    }
    .content {
        width: 90%;
        margin: 0 auto;
    }
    #searchid { 
        width: 90%;
        border: solid 1px #000;
        padding: 10px;
        font-size: 14px;
    }
    #result {
        position: absolute;
        width: 70%;
        padding: 15px;
        display: none;
        margin-top: -1px;
        border-top: 0px;
        overflow: hidden;
        border: 1px #CCC solid;
        color: white;
        background-color: black;
    }
    .show {
        padding: 4px; 
        border-bottom: 1px #999 dashed;
        font-size: 12px; 
        height: 58px;
    }
    .show:hover {
        background: #4c66a4;
        color: #FFF;
        cursor: pointer;
    }
</style>

Search.php

<?php
    include('admin/db.php');
    if($_POST)
    {
        $q = $_POST['search'];
        $sql_res = mysql_query("select id, name, email, infos from autocomplete where name like '%$q%' or email like '%$q%' or infos like '%$q%' order by id LIMIT 100");
        while ($row = mysql_fetch_array($sql_res))
        {
            $username = $row['name'];
            $email = $row['email'];
            $infos = $row['infos'];
            $b_username = '<strong><font style="color: black; background-color: #ffff42">'.$q.'</strong></font>';
            $b_email = '<strong><font style="color: black; background-color: #ffff42">'.$q.'</strong></font>';
            $b_infos = '<strong><font style="color: black; background-color: #ffff42">'.$q.'</strong></font>';
            $final_username = str_ireplace($q, $b_username, $username);
            $final_email = str_ireplace($q, $b_email, $email);
            $final_infos = str_ireplace($q, $b_infos, $infos);
        ?>
        <div class="show" align="left">
            <span class="name">
                <?php echo $final_username; ?>
            </span>&nbsp;<br/>
            <?php echo $final_email; ?>
            </span>&nbsp;<br/>
            <?php echo $final_infos; ?><br/>
        </div>
    <?php }
} ?>

Upvotes: 1

Views: 83

Answers (2)

Disfigure
Disfigure

Reputation: 740

you should take look to levenshtein function.

http://php.net/manual/fr/function.levenshtein.php

Try to make your own algo, it will increase your skill.

Upvotes: 1

Alexis
Alexis

Reputation: 5831

i'm french too ! :D

Here you search a query like "name is" you're request search in Name, email, and info column all results who contain exactly "name is".

If you want to search word by word you need to use a boucle for each word in you're search query store in a tempory table the results and return all the result who contain each word.

There's maybe an other solution look here : SQL SELECT WHERE field contains words

Upvotes: 0

Related Questions