Facebook
From Azizbek Qozoqov, 1 Week ago, written in PHP.
Embed
Download Paste or View Raw
Hits: 191
  1. public function search(string $str='',int $son = 5)
  2.   {
  3.     $words = $this->m_explode($str);
  4.     $query = "SELECT * FROM `music` WHERE ";
  5.     $like = " name LIKE '%$str%' OR ";
  6.     foreach($words as $word)
  7.     {
  8.       $like .=" name LIKE '%$word%' OR ";
  9.     }
  10.     $order = " ORDER BY  name LIKE '%$str%' DESC ";
  11.     $query .= rtrim($like,' OR').$order;
  12.     $result = $this->runQuery($query);
  13.     if(!$result) return false;
  14.     if($this->row_exists($result))
  15.     {
  16.       $userInput = $str;
  17.       $list = [];
  18.       while($q = $result->fetch_assoc())
  19.       {
  20.      
  21.       $list[] = $q['name'];
  22.       }
  23.  
  24.       usort($list, function ($a, $b) use ($userInput)
  25. {
  26.  similar_text($userInput, $a, $percentA);
  27. similar_text($userInput, $b, $percentB);
  28.  return $percentA === $percentB ? 0 : ($percentA > $percentB ? -1 : 1);
  29. }
  30. );
  31.  
  32. return $list;
  33. }
  34.     return false;
  35.   }