【问题标题】:JQuery Autocomplete with Geonames and MYSQL使用 Geonames 和 MYSQL 的 JQuery 自动完成
【发布时间】:2014-04-07 12:36:10
【问题描述】:

我试图创建一个多功能搜索栏,不仅可以查找城市(使用 Geonames),还可以使用相同的输入字段来查找注册用户(使用我的 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");
    }
})


});

搜索.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);

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

【问题讨论】:

    标签: php jquery mysql autocomplete


    【解决方案1】:

    您需要执行一项服务以从多个服务中获取信息,将它们组合起来并响应您的自动完成结果。假设您有一个名为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);
    

    在js端;

    $(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");
            }
        });
    });
    

    【讨论】:

    • 效果很好!是否有可能混合结果,因为现在首先显示用户然后显示城市?
    • 您可以更改 php 文件中函数的顺序。能否请您更改该顺序并重试?
    • 如果我改变顺序,那么首先是城市,然后是用户。但是我希望它混合,所以你有一个用户,然后是一个城市,然后可能是两个用户,然后是一个城市..它应该有点随机
    • @user2072953 例如,我在 php 端对结果数组进行了排序,结果将与用户和城市混合。查看我的更新答案
    猜你喜欢
    • 2013-07-11
    • 2012-07-29
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 2011-07-15
    相关资源
    最近更新 更多