I am making use of jQuery's .autocomplete plugin, and I would appreciate some assistance formating the results that show up in the dropdown menu that populates when text is written into the input field.
In order to get the data to populate the autocomplete, I pull from mySQL using this PHP:
Expand|Select|Wrap|Line Numbers
- $sql = ( 'SELECT tag, title, author, content, id FROM labels' );
- $result = mysql_query( $sql );
- $Response = array();
- while( $row = mysql_fetch_object( $result ) ){
- $Response[] = array(
- "id" => $row->id,
- "name" => $row->tag . ": " . $row->title . ": " . $row->content .""
- );
- }
Expand|Select|Wrap|Line Numbers
- var AllTagData = <?= json_encode ( $Response ); ?>;
- var CRs = [];
- for(var i in AllTagData){
- Tags.push(AllTagData[i].name);
- }
- function getIdFromTag(_name){
- for(var i in AllTagData){
- if(_name == AllTagData[i].name){
- return AllTagData[i].id;
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- $(document).ready(function(){
- $("#Responses").autocomplete({
- source: Tags,
- matchContains: true,
- autoFill: true,
- select: function(e, ui){
- $("#hidden_tags").val( getIdFromTags($("#Responses").val()) );
- }});
- });
From the PHP given above, I output Tags, Title, and Content all in the "name". The way that I would like to format my autocomplete options for the user is
- Show Tags & Title
- Hide Content
- Search through Tags, Title, and Content for possible matches
I've been having a lot of trouble with this and have searched quite extensively to find an answer and would really appreciate any help you can give on how to accomplish this. Please ask any follow up questions if you need further clarification. Thanks!