【问题标题】:Search suggestion box inputs space in front of search query搜索建议框在搜索查询前面输入空格
【发布时间】:2012-02-13 01:01:16
【问题描述】:

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

我们用于建议的代码是这样的:

$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.';

【问题讨论】:

  • $queryString 来自哪里? (显示代码)

标签: php mysql autosuggest


【解决方案1】:

用 trim() 试试

$queryString = trim($queryString);

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

【讨论】:

  • 这是我的查询字符串的来源:(我添加了修剪,但这不起作用)//是否有发布的查询字符串? if(isset($_POST['queryString'])) { $queryString = trim($db->real_escape_string($_POST['queryString']));
【解决方案2】:

尝试使用下面指定的Sameera Thilakasiri 的 trim() 函数,并将您的查询更新为类似"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' 

希望有帮助!

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    相关资源
    最近更新 更多