简体   繁体   English

如何在预先输入的Bootstrap中使用通过MySQLi从数据库中获取的数据?

[英]How can I use the data I get from my database using MySQLi, in Bootstrap typeahead?

I'm using jQuery ajax + MySQLi's prepared statements to get data from my database. 我正在使用jQuery ajax + MySQLi的准备好的语句从数据库中获取数据。 The problem is that I don't know how to exactly format the data to use in Bootstrap's typeahead plugin. 问题是我不知道如何精确格式化要在Bootstrap的typeahead插件中使用的数据。

This is the relevant code: 这是相关代码:

PHP: PHP:

$stmt = $mysqli->prepare("SELECT GAME_NAME FROM GAMES WHERE GAME_NAME LIKE ?");
        $query = "%".$query."%";
        $stmt->bind_param('s',$query);
        $stmt->execute();
        $stmt->store_result();
        $count = $stmt->num_rows;

        if($count > 0) {

          $stmt->bind_result($result);
           while($stmt->fetch()) {

            echo json_encode($result);
          }

What I get as AJAX response is all the names as a bunch of text: 我得到的AJAX响应就是所有名称,都是一堆文本:

'"game 1""game 2""blaablaa""some other game"....'

I think I have to have an array of names and I don't know how to get the stmt result as an array. 我想我必须要有一个名称数组,而且我不知道如何将stmt结果作为数组来获取。 The example I tried and works is (I use the array allCities as data-source): 我尝试并工作的示例是(我使用数组allCities作为数据源):

<script type="text/javascript">
        $(document).ready(function() {
            var allCities = ['Baltimore', 'Boston', 'New York', 'Tampa Bay', 'Toronto', 'Chicago', 'Cleveland', 'Detroit', 'Kansas City', 'Minnesota', 'Los Angeles', 'Oakland', 'Seattle', 'Texas'].sort();
            $('#city').typeahead({source: allCities, items:5});
        });
    </script>

Now if I only could get result in the same format as in the example, my problem should be solved, I think. 现在,我认为,如果我只能以与示例相同的格式获得结果,则应该解决我的问题。 Btw, I'm not sure about the json_encode() I used in the code. 顺便说一句,我不确定代码中使用的json_encode() That's just something I gave a try. 那只是我尝试过的东西。 I appreciate any help. 感谢您的帮助。 Thanks. 谢谢。

UPDATE, Ajax: 更新,Ajax:

function handleSearch() {

    var query = $.trim($('#search-field').val());
    var itm = getSearchItem();

    $.ajax({

        type: "POST",
        url: "..//functions/handleSearch.php",
        dataType: "json",
        data: "query="+query+"&itm="+itm,
        success: function(resp) {

            console.log("Server said (response):\n '" + resp + "'");

            $('#search-field').typeahead({source: resp});


        },

        error: function(e) {
            console.log("Server said (error):\n '" + e + "'");
        }
    });

another update: 另一个更新:

In Network tab the response gives the result I want but in this format: Resistance: Fall of ManResident Evil 4John Woo Presents StrangleholdAge of Empires II: The Age of KingsResident Evil 2 . 在“网络”选项卡中,响应以以下格式给出了我想要的结果: Resistance: Fall of ManResident Evil 4John Woo Presents StrangleholdAge of Empires II: The Age of KingsResident Evil 2 So without any formatting. 因此无需任何格式。 Console.log(resp) gives me nothing though. Console.log(resp)虽然没有给我任何东西。 Although when I search for "resident evil 6", that means when I type in the EXACT NAME, console.log also works. 虽然当我搜索“邪恶居民6”时,这意味着当我键入“确切名称”时, console.log也可以使用。

post the code that initializes ajax request. 发布初始化ajax请求的代码。

For example this is shorthand for jquery ajax function 例如,这是jquery ajax函数的简写

$.ajax({
    url: url,
    dataType: 'json',
    data: data,
    success: callback
});

if data type specified json then callback function will receive an array like allCities in your example then you can pass it to your plugin. 如果数据类型指定为json,则回调函数将在您的示例中收到类似于allCities的数组,则可以将其传递给插件。 For example pseudo code: 例如伪代码:

$.ajax({
  url: 'http://blabla',
  dataType: 'json',
  data: dataArray,
  success: function(response) {
    $('#city').typeahead({source: response, items:response.count()});
  }
});

Basically you should create key=>value store array and then in the end you should output it with json_encode. 基本上,您应该创建key => value store数组,然后最后应使用json_encode将其输出。 What you are doing wrong in your code is you are trying to echo and json_encode on every result which should be done just in the end. 您在代码中做错的是,您尝试对每个结果进行回显和json_encode,这些结果应在最后完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM