0

我正在尝试从这里使用 Jquery 的自动完成功能http://jqueryui.com/autocomplete/#default

但我的输出不适用于它。

这是我的小提琴。我不得不手动将 json 插入到数组中,因为 JsFiddle 不允许外部数据库的...

PHP:

<?php 
  include('../../dbconn.php');
  //--------------------------------------------------------------------------
  // Example php script for fetching data from mysql database
  //--------------------------------------------------------------------------
  $databaseName = "accounting";
  $tableName = "generalTransactions";

  //--------------------------------------------------------------------------
  // 1) Connect to mysql database
  //--------------------------------------------------------------------------

  $con = mysql_connect($gaSql['server'],$gaSql['user'],$gaSql['password']);
  $dbs = mysql_select_db($databaseName, $con);
  //--------------------------------------------------------------------------
  // 2) Query database for data
  //--------------------------------------------------------------------------

  $result = mysql_query("SELECT * FROM $tableName ORDER BY `id` DESC LIMIT 1 ");          //query
  $array = mysql_fetch_row($result);                          //fetch result                          
  //--------------------------------------------------------------------------
  // 3) echo result as json 
  //--------------------------------------------------------------------------
  echo json_encode($array);

?>

小提琴中的阿贾克斯:

$(function() {
    var availableTags = [
        ["23","1500-0900","Profit On Sale Of Fixed Assets","Income Statement","Other Income","","","2013-07-26 10:22:07","Demo Admin"],
        ["13","1500-0920","Profit On Sale Of Fixed Assets","Income Statement","Other Income","","","2013-07-23 13:42:45","Demo Admin"],
        ["14","3100-1200","Other Assets 002 - Owned - Depreciation","Income Statement","Expenditure","Depreciation","","2013-07-23 13:47:06","Demo Admin"],
        ["12","3200-1120","Other Assets 001 - Leased - Depreciation","Income Statement 3","Expenditure","Depreciation","","2013-07-23 13:48:42","Demo Admin"],
        ["16","3300-0800","Bank Charges","Income Statement","Expenditure","","","2013-07-25 10:27:16","Demo Admin"],
        ["15","3300-4100","Loss On Sale Of Fixed Assets","Income Statement","Expenditure","","","2013-07-25 10:27:08","Demo Admin"],
        ["21","3300-5500","Rental - Premises","Income Statement","Expenditure","","","2013-07-25 10:27:53","Demo Admin"],
        ["20","3300-6800","Transport And Freight","Income Statement","Expenditure","","","2013-07-25 10:27:47","Demo Admin"],
        ["22","6500-5100","Goodwill - Impairment","Balance Sheet","Non-Current Assets","Goodwill","","2013-07-25 10:28:00","Demo Admin"],
        ["17","7400-0100","Interbank Transfer","Balance Sheet","Current Assets","Cash And Cash Equivalents","","2013-07-25 10:27:22","Demo Admin"],
        ["18","7400-0200","Bank Balance 001","Balance Sheet","Current Assets","Cash And Cash Equivalents","","2013-07-25 10:27:29","Demo Admin"],
        ["19","9800-0100","Taxation - Normal","Balance Sheet","Current Liabilities","Taxation","","2013-07-25 10:27:40","Demo Admin"]
    ];
    $( "#account" ).autocomplete({
        source: availableTags
    });
});

我的文件中的 Ajax:

$( "#account" ).autocomplete({
    source: 'search.php'
});

我不认为我的输出是正确的......

如果您有任何答案或建议,我将不胜感激!

更新:

新输出使用mysql_fetch_accoc

[{"id":"23","accountNumber":"1500-0900","accountDescription":"Profit On Sale Of Fixed Assets","accountLevel1":"Income Statement","accountLevel2":"Other Income","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-26 10:22:07","createdUser":"Demo Admin"},{"id":"13","accountNumber":"1500-0920","accountDescription":"Profit On Sale Of Fixed Assets","accountLevel1":"Income Statement","accountLevel2":"Other Income","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-23 13:42:45","createdUser":"Demo Admin"},{"id":"14","accountNumber":"3100-1200","accountDescription":"Other Assets 002 - Owned - Depreciation","accountLevel1":"Income Statement","accountLevel2":"Expenditure","accountLevel3":"Depreciation","accountLevel4":"","createdDate":"2013-07-23 13:47:06","createdUser":"Demo Admin"},{"id":"12","accountNumber":"3200-1120","accountDescription":"Other Assets 001 - Leased - Depreciation","accountLevel1":"Income Statement 3","accountLevel2":"Expenditure","accountLevel3":"Depreciation","accountLevel4":"","createdDate":"2013-07-23 13:48:42","createdUser":"Demo Admin"},{"id":"16","accountNumber":"3300-0800","accountDescription":"Bank Charges","accountLevel1":"Income Statement","accountLevel2":"Expenditure","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-25 10:27:16","createdUser":"Demo Admin"},{"id":"15","accountNumber":"3300-4100","accountDescription":"Loss On Sale Of Fixed Assets","accountLevel1":"Income Statement","accountLevel2":"Expenditure","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-25 10:27:08","createdUser":"Demo Admin"},{"id":"21","accountNumber":"3300-5500","accountDescription":"Rental - Premises","accountLevel1":"Income Statement","accountLevel2":"Expenditure","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-25 10:27:53","createdUser":"Demo Admin"},{"id":"20","accountNumber":"3300-6800","accountDescription":"Transport And Freight","accountLevel1":"Income Statement","accountLevel2":"Expenditure","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-25 10:27:47","createdUser":"Demo Admin"},{"id":"22","accountNumber":"6500-5100","accountDescription":"Goodwill - Impairment","accountLevel1":"Balance Sheet","accountLevel2":"Non-Current Assets","accountLevel3":"Goodwill","accountLevel4":"","createdDate":"2013-07-25 10:28:00","createdUser":"Demo Admin"},{"id":"17","accountNumber":"7400-0100","accountDescription":"Interbank Transfer","accountLevel1":"Balance Sheet","accountLevel2":"Current Assets","accountLevel3":"Cash And Cash Equivalents","accountLevel4":"","createdDate":"2013-07-25 10:27:22","createdUser":"Demo Admin"},{"id":"18","accountNumber":"7400-0200","accountDescription":"Bank Balance 001","accountLevel1":"Balance Sheet","accountLevel2":"Current Assets","accountLevel3":"Cash And Cash Equivalents","accountLevel4":"","createdDate":"2013-07-25 10:27:29","createdUser":"Demo Admin"},{"id":"19","accountNumber":"9800-0100","accountDescription":"Taxation - Normal","accountLevel1":"Balance Sheet","accountLevel2":"Current Liabilities","accountLevel3":"Taxation","accountLevel4":"","createdDate":"2013-07-25 10:27:40","createdUser":"Demo Admin"}]

编辑:

[{"id":"23","accountNumber":"1500-0900","accountDescription":"Profit On Sale Of Fixed Assets","accountLevel1":"Income Statement","accountLevel2":"Other Income","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-26 10:22:07","createdUser":"Demo Admin","label":"1500-0900","value":"23"}]
4

1 回答 1

1

您可以通过以下两种方式查看:

a) 您的输出需要进行处理,以便查找自动完成脚本需要显示的内容; b) 您可以在“自动完成”选项顶部修改脚本的响应,search.php使其工作。

无论哪种方式,来自的回复search.php都需要是一个关联数组,所以请更改mysql_fetch_rowmysql_fetch_assoc. 我们将使用第一种方式 {a} 并让它变得很棒!

现在,让我们看看自动完成脚本需要什么才能工作。您正在返回似乎是一个对象数组:

var AvailableTags = 
{"id":"23","accountNumber":"1500-0900","accountDescription":"Profit On Sale Of Fixed Assets","accountLevel1":"Income Statement","accountLevel2":"Other Income","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-26 10:22:07","createdUser":"Demo Admin"}
{"id":"13","accountNumber":"1500-0920","accountDescription":"Profit On Sale Of Fixed Assets","accountLevel1":"Income Statement","accountLevel2":"Other Income","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-23 13:42:45","createdUser":"Demo Admin"}
{"id":"14","accountNumber":"3100-1200","accountDescription":"Other Assets 002 - Owned - Depreciation","accountLevel1":"Income Statement","accountLevel2":"Expenditure","accountLevel3":"Depreciation","accountLevel4":"","createdDate":"2013-07-23 13:47:06","createdUser":"Demo Admin"}
{"id":"12","accountNumber":"3200-1120","accountDescription":"Other Assets 001 - Leased - Depreciation","accountLevel1":"Income Statement 3","accountLevel2":"Expenditure","accountLevel3":"Depreciation","accountLevel4":"","createdDate":"2013-07-23 13:48:42","createdUser":"Demo Admin"}
{"id":"16","accountNumber":"3300-0800","accountDescription":"Bank Charges","accountLevel1":"Income Statement","accountLevel2":"Expenditure","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-25 10:27:16","createdUser":"Demo Admin"}
{"id":"15","accountNumber":"3300-4100","accountDescription":"Loss On Sale Of Fixed Assets","accountLevel1":"Income Statement","accountLevel2":"Expenditure","accountLevel3":"","accountLevel4":"","createdDate":"2013-07-25 10:27:08","createdUser":"Demo Admin"}
...
{"id":"19","accountNumber":"9800-0100","accountDescription":"Taxation - Normal","accountLevel1":"Balance Sheet","accountLevel2":"Current Liabilities","accountLevel3":"Taxation","accountLevel4":"","createdDate":"2013-07-25 10:27:40","createdUser":"Demo Admin"}]

不幸的是,这些对象缺少两个关键标识符来告诉自动完成脚本如何正确使用它们。itemvalue键。这些键告诉自动完成您的响应的哪些部分显示在输入框中,以及将哪些部分用作该选择的值。例如,在您的示例(如下)中,我们将需要为您的返回对象的两个元素分配一个项目键和一个键 - 为您返回的数组中的每个对象分配一对。

$( "#account" ).autocomplete({
    source: 'search.php'
})

jsFiddle DEMO (快到了!)

于 2013-08-16T09:20:42.397 回答