Jetoox
Jetoox

Reputation: 1437

Display two fields from database table to populate jquery autocomplete

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?

Upvotes: 1

Views: 1194

Answers (3)

Wazan
Wazan

Reputation: 539

try this your sql query..

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

Upvotes: 0

Sergey
Sergey

Reputation: 5207

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

Upvotes: 2

nnnnnn
nnnnnn

Reputation: 150040

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

Upvotes: 2

Related Questions