我正在尝试在cakephp 3.x中执行实时搜索,到目前为止,我能够通过静态定义脚本标记内的数据来实现它,我希望从我的MySql数据库中检索数据,其中所有内容都存储起来。下面是我在Template文件夹中使用的脚本标记。
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
<script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<!-- Javascript -->
<script>
$(function() {
var results = [
"ActionScript",
"Boostrap",
"C",
"C++",
];
$( "#automplete-1" ).autocomplete({
source: results
});
});
&#13;
<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
rel = "stylesheet">
&#13;
<html>
<body>
<!-- HTML -->
<div class = "ui-widget">
<?php foreach($data as $row)
{
echo $row->name;
}?>
<p>Type "a" or "s"</p>
<label for = "automplete-1">Tags: </label>
<input id = "automplete-1">
</div>
</body>
</html>
&#13;
答案 0 :(得分:1)
首先,您必须从$ data数组中仅提取名称。你可以使用CakePHP Hash :: extract函数(http://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake \ Utility \ Hash :: extract)来完成它。然后将结果编码为JSON并将其打印出来。像
这样的东西var results = <?php echo json_encode(\Cake\Utility\Hash::extract($data, '{n}.name')); ?>;