0

I am having two problems. Right now my autocomplete is showing all 600 results no matter what I type in it never narrows down to fewer results. I also need it to display only the closest 5 results that match what I am typing. Any ideas on how to improve this.

PHP

<?php
include('inc/connections.php');




    $sql = "select top 5 a.item_desc_1, b.item_no from bmprdstr_sql b, imitmidx_sql a 
                    where a.item_no=b.comp_item_no and b.user_def_fld = 'x'
                    AND b.item_no like '%{$_POST['auto_me']}%' 
";


    $query = odbc_exec($conn, $sql);
    $json = array();
    while ($row = odbc_fetch_array($query)) {

        $json[] = $row['item_no'];

    }
    echo json_encode($json);

JQUERY HTML

<html>
<head>
    <title>
        test
    </title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<script>
    $(document).ready(function(){
        $(function() {
            $("#auto_me").autocomplete({
                source: 'auto_complete.php',
                autoFocus:true

            });
        });
    });


</script>
</head>




<body>
<form id="auto">
<input type="text" id="auto_me" name="auto_me"/>
    </form>
</body>
</html>
Ryan
  • 303
  • 5
  • 16

1 Answers1

0
$("#auto_me").autocomplete({
    source: function(request, response) {
        var results = $.ui.autocomplete.filter(myarray, request.term);
        response(results.slice(0, 10));
    }
});

Limit results in jQuery UI Autocomplete

Community
  • 1
  • 1
SGalea
  • 712
  • 9
  • 18
  • So I would just need to replace myarray with auto_complete.php? – Ryan Jul 14 '16 at 15:31
  • You can either get all the results and results.slice(0, 5); at client side or always return the first 5 from inside auto_complete.php. http://php.net/manual/en/function.array-slice.php. using echo json_encode(array_slice($json, 5)); I prefer the second if the result set is very large. – SGalea Jul 15 '16 at 06:08