我怎样才能得到我的自动建议与mysql工作


How can i get my autosuggest to work with mysql

我试图得到一些数据mysql到我的自动建议在我的javascript。问题很奇怪:程序运行正常,但是当您在文本框中写入值时,提示弹出"empty".

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

支持两种格式:

字符串数组:["Choice1", "Choice2"]

具有label和value属性的对象数组:[{label: "Choice1", value:"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); ?>;