简体   繁体   中英

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 for 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM