mysql query returns only one word of string

I am using php and javascript to create a dynamicly populated autocomplete search engine for my wordpress site.

Example: user wants to search for “Nike Shoes” so they type in Nike Shoes into the search engine and they should see Nike Shoes as a suggestion after they type the first 3 letters.

Read More

I am having an issue where only the word Nike shows up as a suggestion. This happens with any terms you type into the search engine. Only the first word of the string will show up. I am sure it’s to do with the spaces in between words but not so sure on how to fix it.

Here is my code that is populating the autocomplete dynamically:

var availableTags = '<?php $result = mysql_query("select * from state"); 
                    $row = mysql_fetch_array($result); 
                    echo $row['name']; ?>'.split(" ");
                    $( "#title" ).autocomplete({
                    source: availableTags,
                    minLength: 3,
                    position: { my : "left top", at: "left top" },
                    appendTo: "#search-container"
                    });
                    });

I created this code from this site: http://www.tizag.com/mysqlTutorial/mysqlquery.php

If someone could please help me I would be very greatful.

Thanks,

Steve

Related posts

Leave a Reply

1 comment

  1. I am using this function for auto suggest list. you can use it with slight modification

    function get_autosuggest_list($str=''){
    
            $q     = strtolower($str);
            $entry = array();   
            $i=0;
            if(!empty($q))
            {
                $entry[$i] = "A.fieldname like '".$q ."%'" ;
                $i++;
            }
    
            $sqlentry = "";
            $j=0;
            foreach($entry as $data)
            {
    
                if($j==0)
                $sqlentry  = " where ".$data;
                else
                $sqlentry .= " and ".$data;
    
                $j++;
            }
            $sql    = "SELECT DISTINCT field_name FROM `table` A ".$sqlentry." ";
            $query  = mysql_query($sql);
            $res    = mysql_fetch_array($query);
    
            foreach($res as $row)
            {
                $items = $row->fieldname;
                echo "$itemsn";
            }
    
        }
    

    Usage:

    get_autosuggest_list(string);