0

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

我制作了一个搜索功能,允许用户搜索一个问题,它将通过计算问题中匹配单词的数量来显示前 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){
        $finalarray = array($title => $matchingwords);

        foreach( $finalarray as $thetitle=>$countmatch ){


?>
    <li><a href="#"><?php echo $thetitle ?><i> &nbsp; <br />No. of matching words: <?php echo $countmatch; ?></i></a></li>
<?php


                }
                $count++;
        if ($count == 5) {break;
        }
                }else{



            }
    }
echo "</ul>";
}

?>
当您搜索某些内容时,它会显示类似这样的内容。
在此处输入图像描述
Iv 将匹配单词的数量放在每个问题下,但它们不按顺序排列。它仅显示数据库中具有 50% 单词匹配的前 5 个问题。我希望它显示匹配词最多的前 5 个。
我需要添加什么代码,我应该把它放在哪里才能做到这一点?
谢谢

4

2 回答 2

1

这是我对你的问题的看法。很多东西都变了:

  • mysql_用PDO替换的功能
  • 使用匿名函数意味着需要 PHP 5.3
  • 主要逻辑已被重组(很难跟踪您的结果处理,所以我可能会遗漏您需要的东西,例如那个点$percentage

我意识到这可能看起来很复杂,但我认为你越早学习现代实践(PDO、匿名函数),你就会越好。

<?php
/**
 * @param string $search_term word or space-separated list of words to search for
 * @param int $count
 * @return stdClass[] array of matching row objects
 */
function find_matches($search_term, $count = 5) {
    $search_term = str_replace("?", "", $search_term);
    $search_term = trim($search_term);
    if(!strlen($search_term)) {
        return array();
    }
    $search_terms = explode(" ", $search_term);

    // build query with bind variables to avoid sql injection
    $params = array();
    $clauses = array();

    foreach ($search_terms as $key => $word) {
        $ident = ":choice" . intval($key);
        $clause = "`title` LIKE {$ident}";
        $clauses []= $clause;
        $params [$ident] = '%' . $word . '%';
    }

    // execute query
    $pdo = new PDO('connection_string');
    $q = "SELECT * FROM `posts` WHERE  " . implode(' OR ', $clauses);
    $query = $pdo->prepare($q);
    $query->execute($params);
    $rows = $query->fetchAll(PDO::FETCH_OBJ);

    // for each row, count matches
    foreach($rows as $row) {
        $the_title = $row->title;
        $the_title = str_replace("?", "", $the_title);

        $title_terms = explode(" ", $the_title);
        $result = array_intersect($search_terms, $title_terms);
        $row->matchcount = count($result);
    }

    // sort all rows by match count descending, rows with more matches come first
    usort($rows, function($row1, $row2) {
        return - ($row1->matchcount - $row2->matchcount);
    });

    return array_slice($rows, 0, $count);
}
?>

<?php
    $search_term = filter_var($_GET["s"], FILTER_SANITIZE_STRING);
    $best_matches = find_matches($search_term, 5);
?>

<?php if(count($best_matches)): ?>
    <ul>
    <?php foreach($best_matches as $match): ?>
        <li><a href="#"><?php echo htmlspecialchars($match->title); ?><i> &nbsp; <br/>No. of matching words: <?php echo $match->matchcount; ?></i></a></li>
    <?php endforeach; ?>
    </ul>
<?php else: ?>
    <div id="search-status">No result found!</div>
<?php endif; ?>
于 2012-08-05T14:50:31.723 回答
0

尝试asort($finalarray);$finalarray = array($title => $matchingwords);声明后添加:

...

if ($matchingwords >= $percent){
    $finalarray = array($title => $matchingwords);
    asort($finalarray);

....

它应该按值对数组进行升序排序

于 2012-08-05T13:58:01.707 回答