如何对此代码的结果进行排序?

时间:2012-08-04 13:35:55

标签: php search sorting

即时创建一个允许用户输入问题的搜索功能,我的代码将尽可能多地匹配我的MySQL数据库中已有的问题,并显示前5个结果,具体取决于单词的数量在问题中匹配。

我使用count()函数计算匹配单词的数量,但是此时显示的结果显示为数据库中具有50%单词匹配或更多的前5个结果。我希望结果首先显示为最高匹配,并为数据库中的每个结果向下显示,但仅显示前5个。 这是我的代码

<?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
    $search_count = str_word_count($search_term);  //count words of string entered by user
    $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'];
            $percentage = '.5'; //percentage to take of search word count
            $percent = $search_count - ($search_count * $percentage); //take percentage off word count
            if ($matchingwords >= $percent){

                ?>
            <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>";
    }
?>

下图显示了当我在搜索栏中搜索“如何制作我自己的网站”时会发生什么。我已经在数据库中有几个问题用于测试,这些问题都是类似的问题,最后一个条目与我问的问题完全匹配,但是由于它目前显示为前5个mathing结果,它忽略了完整匹配。 以下是该搜索的结果。 enter image description here

我添加了一些代码,显示每个问题中有多少单词匹配,这样你就可以看到它运行得更好了。它也是一个巧合,它按升序排列,显示数据库中的前5个匹配结果。

我需要添加哪些代码来安排它,以便它首先显示与整个数据库最接近的匹配,然后是第二个最佳匹配,第三个匹配......?

1 个答案:

答案 0 :(得分:3)

您可以使用嵌套SQL查询,您可以在其中按计数排序或首先将值加载到数组php数组,然后对数组进行排序。使用SQL是非常有效的&amp;&amp;更快。

Multi Dimension Array Sorting

select column1, column2,..., LENGTH(titlecolumn) - LENGTH(REPLACE(titlecolumn, '$search term', '')) AS nummatchwords from posts where " . implode(' OR ', $array) order by nummatchwords;