搜索建议框在搜索查询前输入空格

时间:2012-01-18 09:37:59

标签: php mysql autosuggest

我们的搜索建议存在问题。每当我们点击我们网站上的建议时,它会在搜索查询前面放置一个空格,这会导致查询失败。

我们用于建议的代码是:

$query = $db->query("SELECT DISTINCT productnaam FROM product WHERE  merk LIKE      '$queryString%' LIMIT 10");
            if($query) {
                // While there are results loop through them -  fetching an Object (i like PHP5 btw!).
                while ($result = $query ->fetch_object()) {
                    // Format the results, im using <li> for the list, you can change it.
                    // The onClick function fills the textbox with the result.

                    // YOU MUST CHANGE: $result->value to $result->your_colum
                    echo '<li onClick="fill(\''.$result->merk.'&nbsp;'.$result->productnaam.'\');">'
                    .$result->merk.'&nbsp;'.$result->productnaam.''.'</li>';
                }
            } else {
                echo 'ERROR: There was a problem with the query.';

2 个答案:

答案 0 :(得分:0)

尝试修剪()

$queryString = trim($queryString);

trim()函数从字符串的两边删除空格和其他预定义字符。

答案 1 :(得分:0)

尝试将trim()函数设置为下面指定的Sameera Thilakasiri,并将查询更新为类似"SELECT DISTINCT productnaam FROM product WHERE merk LIKE '%$queryString%' LIMIT 10"的内容。双方的百分号将确保您的查询将选择包含您输入的记录记录以您输入开头的记录。

下面是SQL LIKE条件的进一步解释,可能会帮助你

// This query will look for records that start with "sa"    
select * from table where name like 'sa%' 

// This query will look for records that contain "sa"    
select * from table where name like '%sa%' 

// This query will look for records that end with "sa"    
select * from table where name like '%sa' 
希望有所帮助!