Member Avatar for arcticM

I'm trying to use remote data source autocomplete, I have a code that works.
I'm returning an id column from a db table. what I'm trying to do is to have the autocomplete display 2 values from the table row, for example
id1 - this is id 1
id2 - this is id 2

but I can't figure out how to modify my sql and jquery function to add .description to the item object.
i managed to do that when i got all the possible results in one time and put them in js array, but now that my source is not an array, I don't know how to do it.

my jquery function (i added the part that displays the description but clearly the code that adds the description to item obj is missing)

jQuery(function() {
                                    jQuery("#field").autocomplete({
                                        source: "search.php",

                                            focus: function( event, ui ) {
                                            jQuery( "#field" ).val( ui.item.label );
                                            return false;
                                        },
                                        select: function( event, ui ) {
                                            jQuery( "#field" ).val( ui.item.label );
                                            return false;
                                        }
                                    })
                                    .data( "autocomplete" )._renderItem = function( ul, item ) {
                                        return jQuery( "<li>" )
                                            .data( "item.autocomplete", item )
                                            .append( "<a>" + item.label  +" - "+ item.desc + "</a>" )
                                            .appendTo( ul );
                                    };
                                });

the php code that returns 1 column from the db (working) - need to modify the while loop somehow to return both colums, just don't know how to do it correctly

$return_arr = array();
        if(isset($_GET['term'])) {
            $search_string = $_GET['term'];
            $sql = "SELECT * FROM ".DB_NAME.".table WHERE id LIKE '{$search_string}%' LIMIT 20";
            $res = dbquery( $sql );
            while ($row = dbnext( $res, MYSQL_ASSOC )) {
                $row_array = $row['id'];
                //do something with $row['desc']

                 array_push($return_arr,$row_array);
            }
        }

         $return_arr;
        return json_encode($return_arr);

Something like this should do it:

while ($row = dbnext($res, MYSQL_ASSOC)) {
    $return_array[] = array ('label' => $row['id'], 'desc' => $row['desc']);
}
Member Avatar for arcticM

thank you!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.