2009-08-16 16 views
0

私は自分のサイトの基本的な検索機能を構築しています。最も有用な検索では、指定された用語が最も多く表示されていることを認識し、次のようにコード化しました。検索結果の配列を一致数で並べ替えますか?

function search() 
{ 
    $this->page = preg_replace('/[^\w \"\']/i', '', trim($this->page)); 
    preg_match_all('/"(.[^"]+)"|([\w]+)/i', $this->page, $terms); 
    $terms=array_unique($terms[0]); // scrub duplicate search terms 
    $tsql="SELECT reviewSummary, reviewFullText, game.gameName FROM review LEFT JOIN game ON review.gameID = game.gameID "; 
     $constraint="WHERE"; 
     foreach ($terms as $term) 
     { 
      $term=str_replace('"','',$term); 
      $tsql.=" $constraint (reviewSummary LIKE '%".$term."%' OR reviewFullText LIKE '%".$term."%' OR game.gameName LIKE '%".$term."%')"; 
      $constraint="AND"; 
     } 
     $tsql .= "AND game.isPublished = 'y'"; 
     $result = $this->sql->db_query($tsql); 
     if (mysql_num_rows($result)!=0) 
     { 
      while($row = mysql_fetch_array($result)) 
      { 
       $gameName = stripslashes($row['gameName']); 
       $content = strip_tags($row['reviewFullText']) . ' ' . $row['reviewSummary']; 
       $counter = 0; 
       foreach($terms as $term) 
       { 
        $term=str_replace('"','',$term); 
        preg_match_all("/($term)/i", $content, $matches); 
        foreach($matches[0] as $m) 
        { 
         $counter++; 
        } 
       } 
       $found["Games"][$gameName]=$counter; 
      } 
     } 
     print_r($found); 
} 

素晴らしいです。それはではありません doは、最も一致する結果に基づいて結果を並べ替えます。私はどのようにこれを達成するために結果の配列をソートするか分からない、誰も助けることができますか?

答えて

1

ユーザ定義の比較関数を使用して連想配列をソートできるuasort()を見てください。このようなことがあれば...

function cmp($a, $b) { 
    if ($a == $b) { 
     return 0; 
    } 
    return ($a < $b) ? 1 : -1; 
} 

uasort($found["Games"], 'cmp'); 
+0

これはすばらしいものでした。ポールありがとう! – different

関連する問題