排序和substr_count

| 我正在尝试为小型网站提供简单的搜索功能,并在顶部显示最相关的项目
$q = \"SELECT * FROM pages as p WHERE p.content LIKE \'%$searchparam%\' OR p.title LIKE \'%$searchparam%\' LIMIT 500\";       
$r = @mysqli_query ($dbc, $q); // Run the query.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

    $count = substr_count($row[\'content\'], \"$searchparam\");
我猜我需要用$ count中的值对$ row进行排序,但是我不确定该怎么做。谁能帮我解决语法问题?     
已邀请:
更好的解决方案是使用MySq的全文本搜索功能,因为返回的结果将按顺序返回,并且比尝试自己编写它更容易,更可靠。您也可以研究其他搜索引擎,例如Sphinx。 但是,如果要继续使用方法,则应执行以下操作:
$results = array();
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    $count = substr_count($row[\'content\'], \"$searchparam\");
    //add the details you want to the array. You could also just add count
    // to $row and add $row to $results
    $results[] = array(
        \'count\' => $count,
        \'resultid\' => $row[\'id\']
    );
}

//custom sort function that uses the count to order results    
function cmp($a, $b){
    if ($a[\'count\'] == $b[\'count]) {
        return 0;
    }
    return ($a > $b) ? -1 : 1;
}

//sort the array using the sort function
usort($results, \'cmp\');
    

要回复问题请先登录注册