Php 使用Geonames和MYSQL的JQuery自动完成

Php 使用Geonames和MYSQL的JQuery自动完成,php,jquery,mysql,autocomplete,Php,Jquery,Mysql,Autocomplete,我试图创建一个多功能搜索栏,在这里,人们不仅可以使用地理名称查找城市,还可以使用相同的输入字段使用MYSQL表查找注册用户 我设法分别实现了这两个目标,但是我无法将代码合并在一起 geoname自动完成的JQuery代码: $(function() { $( "#top_search_field" ).autocomplete({ source: function( request, response ) { $.ajax({ url: "http

我试图创建一个多功能搜索栏,在这里,人们不仅可以使用地理名称查找城市,还可以使用相同的输入字段使用MYSQL表查找注册用户

我设法分别实现了这两个目标,但是我无法将代码合并在一起

geoname自动完成的JQuery代码:

$(function() {
$( "#top_search_field" ).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "http://api.geonames.org/searchJSON?username=dummie",
            dataType: "jsonp",
            data: {
                featureClass: "P",
                style: "full",
                country: "AT",
                maxRows: 12,
                name_startsWith: request.term
            },
            success: function( data ) {
                response( $.map( data.geonames, function( item ) {
                    return {
                        label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                        value: item.name
                    }
                }));
            }
        });
    },
    minLength: 2,
    select: function (event, ui) {
        var selectedObj = ui.item;
        jQuery("#top_search_field").val(selectedObj.value);
        return false;
    },

    open: function () {
        jQuery(this).removeClass("ui-corner-all").addClass("ui-corner-top");
    },
    close: function () {
        jQuery(this).removeClass("ui-corner-top").addClass("ui-corner-all");
    }
});
});
用于从表中获取用户的代码:

$(function() {

$( "#top_search_field" ).autocomplete({
     source:'php_includes/search.php',
     minLength: 2,
     open: function () {
        jQuery(this).removeClass("ui-corner-all").addClass("ui-corner-top");
    },
    close: function () {
        jQuery(this).removeClass("ui-corner-top").addClass("ui-corner-all");
    }
})


});
search.php

include_once("db_connect.php");

$stmt = $db_connect->query("SELECT user_auth, first_name, last_name, avatar FROM users WHERE (first_name LIKE '%".$_REQUEST['term']."%' OR last_name LIKE '%".$_REQUEST['term']."%') ");

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$results[] = array('label' => utf8_encode($row['first_name'].' '. $row['last_name'] ) );
}

echo json_encode($results);

将代码合并在一起并同时查找城市和用户的最佳方法是什么?

您需要执行一项服务,从多个服务获取信息,将它们合并并响应自动完成结果。假设您有一个名为advanced_search.php的服务。在本文件中

<?php
$keyword = $_GET["keyword"];
$result = array();

$usersReturnedFromService = getUsersFromService($keyword); // Get users from service by using curl

foreach ($usersReturnedFrom as $user) {
    array_push(array("type" => "user", "value" => $user));  
}

$geoNamesReturnedFromService = getGeoNamesFromService($keyword); // Get geonames from service by using curl

foreach ($geoNamesReturnedFromService as $geoname) {
    array_push(array("type" => "geoname", "value" => $geoname));    
}


// When you sort, result will mixed 
function compareArr($a, $b){
    $ad = strtotime($a['value']);
    $bd = strtotime($b['value']);
    return ($ad-$bd);
}

usort($result, 'compareArr');

echo json_encode($result);

很好!是否有可能混合结果,比如现在先显示用户,然后显示城市?您可以更改php文件中函数的顺序。请您更改顺序,然后再试一次好吗?如果我更改顺序,那么首先是城市,然后是用户。但是我希望它是混合的,所以你有一个用户,然后是一个城市,然后可能是两个用户,然后是一个城市..它应该是一点random@user2072953例如,我在php端对结果数组进行了排序,结果将与用户和城市混合。查看我的最新答案
$(function() {
    $( "#top_search_field" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "advanced_search.php?keyword=" + request.term, // put other variables here. I have only put term
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return {
                            label: item.value, // john
                            value: item.type + "|" + item.value // user|john
                        }
                    }));
                }
            });
        },
        minLength: 2,
        select: function (event, ui) {
            var selectedObj = ui.item;
            jQuery("#top_search_field").val(selectedObj.value);
            return false;
        },

        open: function () {
            jQuery(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function () {
            jQuery(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });
});