I have the following data currently feeding JQuery Autocomplete
我有以下数据正在提供JQuery Autocomplete
var network_autocomplete=[
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#network" ).autocomplete({
source: network_autocomplete
});
I need to modify this to retrieve the array of results from a php page. Can somebody help me to modify this so I can in JavaScript do this?
我需要修改它以从php页面检索结果数组。有人可以帮我修改这个,所以我可以在JavaScript中这样做吗?
network_autocomplete=data
Currently, the code retrieves data from an SQLite table in this manner:
目前,代码以这种方式从SQLite表中检索数据:
try {
$db = new PDO('sqlite:/tmp/bacnet.db');
$query = "SELECT net, name, FROM network";
foreach ($db->query($query) as $row) {
$networks[] = array('net' => $row['net']);
}
echo json_encode($networks);
return json_encode($networks);
} catch (PDOException $e) {
echo json_encode(array('message' => 'Could not connect to the database'));
}
3 个解决方案
#1
1
Change $networks[] = array('net' => $row['net']);
to$networks[] = $row['net'];
And you should not have return, unless this is a function (and if it's a function, why it has an echo?)
更改$ networks [] = array('net'=> $ row ['net']);到$ networks [] = $ row ['net'];并且你不应该返回,除非这是一个函数(如果它是一个函数,为什么它有一个echo?)
#2
0
What I do when i need this is put some PHP directly in the javascript, so
我需要的时候做的是将一些PHP直接放在javascript中,所以
foreach ($db->query($query) as $row) {
$networks[] = '{ label: "blah", value: "' . $row['net'] . '" }';
}
$autocomplete = implode(",",$networks);
Then inside the script you can do,
然后你可以在脚本里面做,
var network_autocomplete = [$autocomplete];
$( "#network" ).autocomplete({
source: network_autocomplete
});
I hope this helps
我希望这有帮助
#3
0
You can do the folowing:
您可以执行以下操作:
<?php
$values = array("'foo'", "'bar'", "'baz'"); ?>
<script>
$(function() {
var network_autocomplete = [
<?php echo join(",", $values) ?>
];
...
</script>
#1
1
Change $networks[] = array('net' => $row['net']);
to$networks[] = $row['net'];
And you should not have return, unless this is a function (and if it's a function, why it has an echo?)
更改$ networks [] = array('net'=> $ row ['net']);到$ networks [] = $ row ['net'];并且你不应该返回,除非这是一个函数(如果它是一个函数,为什么它有一个echo?)
#2
0
What I do when i need this is put some PHP directly in the javascript, so
我需要的时候做的是将一些PHP直接放在javascript中,所以
foreach ($db->query($query) as $row) {
$networks[] = '{ label: "blah", value: "' . $row['net'] . '" }';
}
$autocomplete = implode(",",$networks);
Then inside the script you can do,
然后你可以在脚本里面做,
var network_autocomplete = [$autocomplete];
$( "#network" ).autocomplete({
source: network_autocomplete
});
I hope this helps
我希望这有帮助
#3
0
You can do the folowing:
您可以执行以下操作:
<?php
$values = array("'foo'", "'bar'", "'baz'"); ?>
<script>
$(function() {
var network_autocomplete = [
<?php echo join(",", $values) ?>
];
...
</script>