显示数据库表中的两个字段以填充jquery自动完成

显示数据库表中的两个字段以填充jquery自动完成

问题描述:

I have implemented a jquery ui autocomplete with data from database. Right now what I have done is only to display one field. Let's say for example I have a database:

| id | suburb | code
  0     DAR      10
  1     ASD      20
  2     DEF      30
and so on....

my html code:

<form id="auto_test">
<label for="tags">Tags: </label>
<input id="tags" type="text" name="tags" />
<input type="button" id="auto_test_btn" value="save" />

jquery:

jQuery("#tags").autocomplete({
        source: "<?php echo WP_PLUGIN_URL.'/plugin_name/php_file.php'?>"
    });

php :

$t = $_GET['term']; 
$code = $wpdb->get_results(
        "SELECT suburb as label, suburb as value
        FROM Sheet1
        WHERE suburb like '%$t%'
        LIMIT 25
        ",ARRAY_A
        );  
echo(json_encode($code));

The autocomplete displays only the 'suburb' for it is defined as label. What I wanted to do is to display both the suburb and the code.

What would be the nice thing to do?

我已经使用数据库中的数据实现了jquery ui自动完成功能。 现在我所做的只是显示一个字段。 比方说,我有一个数据库: p>

  |  id | 郊区|  code 
 0 DAR 10 
 1 ASD 20 
 2 DEF 30 
依此类推.... 
  code>  pre> 
 
 

我的html代码: p> \ n

 &lt; form id =“auto_test”&gt; 
&lt; label for =“tags”&gt;标签:&lt; / label&gt; 
&lt; input id =“tags”type =“text  “name =”tags“/&gt; 
&lt; input type =”button“id =”auto_test_btn“value =”save“/&gt; 
  code>  pre> 
 
 

p>

jquery: p>

  jQuery(“#tags”)。autocomplete({
 source:“&lt;?php echo WP_PLUGIN_URL。'/  plugin_name / php_file.php'?&gt;“
}); 
  code>  pre> 
 
 

php: p>

  $ t  = $ _GET ['term'];  
 $ code = $ wpdb-&gt; get_results(
“选择郊区作为标签,郊区作为值
来自Sheet1 
 WHERE郊区,如'%$ t%'
 LIMIT 25 
”,ARRAY_A 
)  ;  
echo(json_encode($ code)); 
  code>  pre> 
 
 

自动完成仅显示“郊区”,因为它被定义为标签。 我想要做的是显示郊区和代码。 p>

最好的事情是什么? p> div>

You can concatenate the columns (with an appropriate separator) within your SQL select statement, perhaps something like this:

"SELECT Concat(suburb, ' ', code) as label, suburb as value

"SELECT concat(suburb, ' ', code) as label, suburb as value

try this your sql query..

SELECT suburb as label, code as value
            FROM Sheet1
            WHERE suburb like '%$t%'
            LIMIT 25