如何将该数组从高到低排序? [英] How to order this array to High to Low?

查看:274
本文介绍了如何将该数组从高到低排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何对这段代码的结果进行排序?

Possible Duplicate:
How to sort the results of this code?

我已将此代码设置为允许用户在搜索栏中键入问题的代码.然后这段代码接受了这个问题,并在我的数据库中寻找带有问题的单词.然后,它计算每个问题的匹配单词数.完成后,它会根据匹配的单词数显示最匹配的前4个问题. 但是,此刻它显示从最低单词匹配到最高单词匹配(从低到高)的这些匹配,而我却反之亦然,以便它首先显示最佳匹配(从高到低).如何在这段代码中做到这一点?

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?

    <?php
        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 "<div id='search-status'>No result found!</div>";
        }
        else //result found
        {
        echo "<ul>";

        $title = $row['title'];
        if ($matchingwords >= 4){
        ?>
<li><a href='<?php echo $row['url']; ?>'><?php echo $title ?><i> &nbsp; No. matching words: <?php echo $matchingwords; ?></i></a></li>
<?php
                $count++;
                if ($count == 5) {break;
                }
                    }else{
                    }
                }
            echo "</ul>";
            }
?>

推荐答案

请注意,这需要Mysql版本4 +

全文 sql查询返回值基于相关性.

A fulltext sql query returns values based on relevance.

示例:

SELECT *, MATCH (title)
AGAINST ('$search_term' IN NATURAL LANGUAGE MODE) AS score
FROM posts;

这就是实现它的方式

$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";

这将按相关性顺序返回帖子.可能需要删除$ search_term周围的'',但您可以通过测试弄清楚.

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.

请阅读全文SQL查询和match()函数.一切都清楚地记录在案.

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

这篇关于如何将该数组从高到低排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆