How to replace database with JSON file for Jquery Ui autocomplete?

0

I'm doing a search engine and I'm using Jquery UI autocomplete to bring suggestions from a database. My question is if I can (and if so, how it is done) replace the database with a JSON file because I imagine it must be faster. My current code is:

    jQuery('.search').autocomplete({
        source: function (request, response) {  
            jQuery.getJSON("busco.php?term=" + request.term, function (data) {
                response(jQuery.map(data, function (etiqueta) {                     
                    return {
                        label: etiqueta.nombre,
                        id: etiqueta.Id,
                    };
                }));                    
            });             
        },
        minLength: 2,
        delay: 100,         
    });

My file I'm looking for:

<?php
$mysqli = new mysqli('localhost', 'root', 'root', 'db');
$term=$_GET['term'];
$resultado = $mysqli->query("SELECT Id,nombre FROM etiquetas where nombre LIKE '$term%'");
$json=array();
while($row = $resultado->fetch_assoc())
{
    $json[]=array(
                    'nombre' => utf8_encode($row['nombre']),
                    'Id' => $row['Id'],
                );
}   
print_r(json_encode($json));    

? >

    
asked by Patricio 23.08.2018 в 21:10
source

0 answers