我怎样才能让我的autosuggest与mysql一起工作
问题描述:
我想从我的javascript中获取一些数据到我的autosuggest中。 问题是wird:程序运行正常,但是当你写在文本框中输入值,“空”的建议多达啪啪..我怎样才能让我的autosuggest与mysql一起工作
PHP:
$sqldropdown = $this->EE->db->query("SELECT emd.m_field_id_8 FROM transactions as t
left join exp_members as em on (t.cardid-10000000 = em.member_id)
left JOIN exp_member_data emd on em.member_id = emd.member_id group by emd.m_field_id_8 ASC");
foreach ($sqldropdown->result_array() as $filterofresults)
{
$samletdropdown[]=$filterofresults;
}
foreach ($samletdropdown as $key => $value)
{
$victims[]= array($value['m_field_id_8']);
如果改变这一行这个:$victims= array($value['m_field_id_8'])
;只有数组中的最后一个值出现在我的自动提示中。然后它工作正常!但是,当我将其更改为数组。它会工作,但只有空的建议会出现。 }
的Javascript:
<meta charset="utf-8" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
var availableTags = <?php echo json_encode($victims); ?>;
$("#tags").autocomplete({
source: availableTags
});
});
</script>
HTML:
</div>
<div class="span2 well " style="height:100px;">
<p>
<label for="tags">Search box: </label>
<input type="text" id="tags" />
</p>
</div>
我到底做错了什么?我真的希望你能理解这个问题。如果不是随意问:)
答
根据文档:http://api.jqueryui.com/autocomplete/#option-source
有两种支持的格式:
的字符串数组: “选择1”, “选择2”]
与标签和值的属性的对象数组:[{标签: “选择1”,值: “VALUE1”},...]
因此,你需要创建数组是这样的:
foreach ($samletdropdown as $key => $value)
{
$data = array();
$data['label'] = 'Label for data : '.key;
$data['value'] = $value['m_field_id_8'];
$victims[] = $data;
//Or directly but without redefining a new array
$victims[] = $value['m_field_id_8'];
}
使用像你之前已经做:
var availableTags = <?php echo json_encode($victims); ?>;