【问题标题】:How can i get my autosuggest to work with mysql我怎样才能让我的自动建议与 mysql 一起工作
【发布时间】:2013-03-05 20:24:04
【问题描述】:

我正在尝试从 mysql 中获取一些数据到我的 javascript 中的自动提示中。 问题很奇怪:程序运行正常,但是当你在文本框中写一个值时,会弹出“空”的提示..

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>  

我做错了什么?我真的希望你能理解这个问题。如果不放心问:)

【问题讨论】:

    标签: php jquery jquery-ui-autocomplete autosuggest


    【解决方案1】:

    根据文档:http://api.jqueryui.com/autocomplete/#option-source

    有两种支持的格式:

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

    具有标签和值属性的对象数组: [ { label: "Choice1", value: "值1" }, ... ]

    因此,您需要像这样创建数组:

      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); ?>;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-28
      • 2013-08-24
      • 2017-12-24
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多