php - Ajax: How to output data for Jquery autocomplete -
i'm trying use jquery's autocomplete here http://jqueryui.com/autocomplete/#default
but output not working it.
here's fiddle. had manually insert json array jsfiddle doesn't allow outside database's...
php:
<?php include('../../dbconn.php'); //-------------------------------------------------------------------------- // example php script fetching data mysql database //-------------------------------------------------------------------------- $databasename = "accounting"; $tablename = "generaltransactions"; //-------------------------------------------------------------------------- // 1) connect mysql database //-------------------------------------------------------------------------- $con = mysql_connect($gasql['server'],$gasql['user'],$gasql['password']); $dbs = mysql_select_db($databasename, $con); //-------------------------------------------------------------------------- // 2) query database data //-------------------------------------------------------------------------- $result = mysql_query("select * $tablename order `id` desc limit 1 "); //query $array = mysql_fetch_row($result); //fetch result //-------------------------------------------------------------------------- // 3) echo result json //-------------------------------------------------------------------------- echo json_encode($array); ?> ajax in fiddle:
$(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 , 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 , cash equivalents","","2013-07-25 10:27:22","demo admin"], ["18","7400-0200","bank balance 001","balance sheet","current assets","cash , 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 in file:
$( "#account" ).autocomplete({ source: 'search.php' }); i dont think output correct...
if have answers or suggestions, i'll appreciate it!
update:
new output using 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 , 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 , 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 , 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"}] edit:
[{"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"}]
you can @ 2 ways:
a) output needs massaged looking autocomplete script needs display; or b) can modify response search.php script within autocomplete options top make work.
either way, reply search.php needs associative array, please change mysql_fetch_row mysql_fetch_assoc. we're going use first way {a} , make fabulous!
now, let's @ autocomplete script needs in order work. returning appears array of objects:
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"}] unfortnately, these objects missing 2 key identifiers tell autocomplete script how use them properly. item , value keys. these keys tell autocomplete parts of responses display in input box , use value selection. instance, in example (below), need assign 2 elements of returning objects item key , value key - pair of these each of objects in returned array.
$( "#account" ).autocomplete({ source: 'search.php' }) jsfiddle demo (almost there!)
Comments
Post a Comment