【发布时间】:2013-08-18 15:22:22
【问题描述】:
我正在尝试从这里使用 Jquery 的自动完成功能 http://jqueryui.com/autocomplete/#default
但我的输出无法使用它。
Here's my FIDDLE. 我不得不手动将 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);
?>
小提琴中的 Ajax:
$(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"}]
【问题讨论】:
-
我可以让您将
$array = mysql_fetch_row($result);更改为$array = mysql_fetch_assoc($result);并告诉我生成的 availableTags 是什么样的吗?谢谢! -
@DevlshOne 我改了,json更好看!请看我的更新!请让我知道我可以对自动完成标签和值做些什么来获取它们?
-
好多了!现在让我告诉你如何使用它......
-
施展你的魔法@DevlshOne
-
好的,再做一个更改..请将
"SELECT * FROM $tableName ORDER BYid` DESC LIMIT 1 "` 更改为:"SELECT *, accountNumber AS label, id AS value FROM $tableName ORDER BY id DESC LIMIT 1 "
标签: php jquery html ajax autocomplete