开发者

array_unique question

I have a search engine type website. It takes the users input, stores the query as $q, explodes the query and searches the database. It then displays the results with the name and web address of each result.

For example, if i searched for "computer programming"... Stack Overflow, stackoverflow.com would be my result. However, it displays twice. (once for computer, and once f开发者_开发百科or programming.)

I tried to solve this with the array_unique function, and it does not work.

any help would be appreciated.

    // trim whitespace

$trimmed = trim($q);
// seperate key-phrases
$trimmed_array = explode(" ", $trimmed);

// remove duplicates

$clean_array = array_unique($trimmed_array);

//query dataabase
foreach ($clean_array as $trimm){
     $query = mysql_query("SELECT * FROM forumlist WHERE `keys` LIKE '%" . mysql_real_escape_string($trimm) . "%' ORDER BY rating DESC, total_ratings DESC") or die(mysql_error());

Thank you!


//query dataabase
$query = 'SELECT * FROM forumlist  ';
$where = array();
foreach ($clean_array as $trimm){
     $where[] = " `keys` LIKE '%" . mysql_real_escape_string($trimm) . "%' ";
}

if(!empty($where)){
    $query .= " WHERE ". implode(' OR ', $where);
}
$query .= " ORDER BY rating DESC, total_ratings DESC";
$result = mysql_query($query) or die(mysql_error());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜