I am trying to write a auto complete, which the auto complete items load one time when the PHP page loads. With the items that are fetched from mysql DB , i have create a json array like this
我正在尝试编写一个自动完成,当PHP页面加载时,自动完成项将加载一次。使用从mysql DB获取的项,我创建了这样的json数组
<?php
$bnkArray = array();
$sql_bnk = mysql_query("SELECT BName, BCode, ID FROM bank");
while($rBnk = mysql_fetch_array($sql_bnk)){
$bnkDet = array(
'label' => $rBnk['BName'],
'value' => $rBnk['BName'],
'otherDetails' => $rBnk['BName'].'||'. $rBnk['BCode'].'||'. $rBnk['ID']
);
array_push($bnkArray, $bnkDet);
}
?>
i need this array as like this javascript array
我需要这个数组,就像这个javascript数组一样
<script>
var bankSource11 = [
{
value: "jquery",
label: "jQuery",
otherDetails: "the write less, do more, JavaScript library",
},
{
value: "jquery-ui",
label: "jQuery UI",
otherDetails: "the official user interface library for jQuery",
},
{
value: "sizzlejs",
label: "Sizzle JS",
otherDetails: "a pure-JavaScript CSS selector engine",
}
];
</script>
if i call this array like this in my auto complete this is not working
如果我像这样在自动补全中调用这个数组,它就不能工作了
var bankSource = [<?php echo $bnkArray; ?>];
what is this array type.. how to do this.
这个数组类型是什么。如何做到这一点。
this is the autocomplete part
这是自动完成的部分
$(this).autocomplete({
minLength: 0,
source: bankSource,
focus: function( event, ui ) {
$(this).val( ui.item.label );
return false;
},
select: function( event, ui ) {
console.log(ui.item.value +' ____ ' + ui.item.otherDetails);
$( "#project" ).val( ui.item.label );
$( "#project-id" ).val( ui.item.value );
$( "#project-description" ).html( ui.item.otherDetails );
return false;
}
})
}
5 个解决方案
#1
3
Please replace
请替换
var bankSource = [<?php echo $bnkArray; ?>];
with
与
var bankSource = <?php echo json_encode($bnkArray); ?>;
#2
2
Here is a sample example which i modified accroding to your requirement. Kindly study it and let me know if you have any question.
这是我根据你的要求修改的一个示例。如果您有任何问题,请仔细研究并告诉我。
<?php
$bnkArray = array();
$sql_bnk = mysql_query("SELECT BName, BCode, ID FROM bank");
while($rBnk = mysql_fetch_array($sql_bnk)){
$bnkDet = array(
'label' => $rBnk['BName'],
'value' => $rBnk['BName'],
'otherDetails' => $rBnk['BName'].'||'. $rBnk['BCode'].'||'. $rBnk['ID']
);
array_push($bnkArray, $bnkDet);
}
?>
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
var bankSource11 = <?php echo json_encode($bnkArray); ?>;
$( "#My_Input" ).autocomplete({
source: bankSource11
});
});
</script>
</head>
<body>
<div class="ui-widget">
<input id="My_Input">
</div>
#3
1
change this line like this,
像这样改变这条线,
var bankSource = [<?php echo $bnkArray; ?>];
to
来
var bankSource = "<?php echo json_encode($bnkArray); ?>";
Php variable is enclosed with quotes and assigned to js variable.
Php变量用引号括起来,并赋给js变量。
#4
0
You should do following :
你应该做以下事情:
var bankSource11 = [<?php echo json_encode($bnkArray); ?>];
#5
0
Note the single quotes, because it's string:
注意单引号,因为它是字符串:
var bankSource = '<?php echo json_encode($bnkArray); ?>';
#1
3
Please replace
请替换
var bankSource = [<?php echo $bnkArray; ?>];
with
与
var bankSource = <?php echo json_encode($bnkArray); ?>;
#2
2
Here is a sample example which i modified accroding to your requirement. Kindly study it and let me know if you have any question.
这是我根据你的要求修改的一个示例。如果您有任何问题,请仔细研究并告诉我。
<?php
$bnkArray = array();
$sql_bnk = mysql_query("SELECT BName, BCode, ID FROM bank");
while($rBnk = mysql_fetch_array($sql_bnk)){
$bnkDet = array(
'label' => $rBnk['BName'],
'value' => $rBnk['BName'],
'otherDetails' => $rBnk['BName'].'||'. $rBnk['BCode'].'||'. $rBnk['ID']
);
array_push($bnkArray, $bnkDet);
}
?>
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
var bankSource11 = <?php echo json_encode($bnkArray); ?>;
$( "#My_Input" ).autocomplete({
source: bankSource11
});
});
</script>
</head>
<body>
<div class="ui-widget">
<input id="My_Input">
</div>
#3
1
change this line like this,
像这样改变这条线,
var bankSource = [<?php echo $bnkArray; ?>];
to
来
var bankSource = "<?php echo json_encode($bnkArray); ?>";
Php variable is enclosed with quotes and assigned to js variable.
Php变量用引号括起来,并赋给js变量。
#4
0
You should do following :
你应该做以下事情:
var bankSource11 = [<?php echo json_encode($bnkArray); ?>];
#5
0
Note the single quotes, because it's string:
注意单引号,因为它是字符串:
var bankSource = '<?php echo json_encode($bnkArray); ?>';