mysql从高到底排序,如何将该数组从高到低排序?

I have made this code which allows a user to type in a search bar a question. This code then takes that question and looks for watching words with questions in my database. It then counts the number of matching words for each question. Once done it then displays the top 4 best matched questions depending on how many words match.

However at the moment it displays these matches from lowest word match to highest word match (low-to-high) and I was it the other way around so that it displays the best match first (high-to-low). How do I do this in this code?

include("config.php");

$search_term = filter_var($_GET["s"], FILTER_SANITIZE_STRING); //User enetered data

$search_term = str_replace ("?", "", $search_term); //remove any question marks from string

$array = explode(" ", $search_term); //Seperate user enterd data

foreach ($array as $key=>$word) {

$array[$key] = " title LIKE '%".$word."%' "; //creates condition for MySQL query

}

$q = "SELECT * FROM posts WHERE " . implode(' OR ', $array); //Query to select data with word matches

$r = mysql_query($q);

$count = 0; //counter to limit results shown

while($row = mysql_fetch_assoc($r)){

$thetitle = $row['title']; //result from query

$thetitle = str_replace ("?", "", $thetitle); //remove any question marks from string

$title_array[] = $thetitle; //creating array for query results

$newarray = explode(" ", $search_term); //Seperate user enterd data again

foreach($title_array as $key => $value) {

$thenewarray = explode(" ", $value); //Seperate each result from query

$wordmatch = array_diff_key($thenewarray, array_flip($newarray));

$result = array_intersect($newarray, $wordmatch);

$matchingwords = count($result); //Count the number of matching words from user entered data and the database query

}

if(mysql_num_rows($r)==0)//no result found{

echo "

No result found!
";

}

else //result found

{

echo "

  • ";

$title = $row['title'];

if ($matchingwords >= 4){

?>

'><?php echo $title ?>   No. matching words: <?php echo $matchingwords; ?>

$count++;

if ($count == 5) {break;

}

}else{

}

}

echo "

";

}

?>

解决方案

note this requires Mysql version 4+

A fulltext sql query returns values based on relevance.

Example:

SELECT *, MATCH (title)

AGAINST ('$search_term' IN NATURAL LANGUAGE MODE) AS score

FROM posts;

This is how you would implement it

$q = "SELECT *,MATCH (title) AGAINST ('$search_term' IN BOOLEAN MODE) AS relevancy FROM

posts WHERE MATCH (title) AGAINST ('$search_term' IN BOOLEAN MODE) ORDER BY

relevancy DESC";

This will return the posts in order by relevancy. May need to remove the '' around $search_term but you can figure that out with testing.

Please read up on Fulltext sql queries and the match() function. It is all clearly documented.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值