I would like to create a search engine that displays results by ranking preset. The rating consists of two parts 1. The amount of post views (represented by POP [INT] in "post" table) and the amount of the words in the text itself (represented by TXT [STRING] in "post" table).
That is if there is a three-Results
//$search = 'apple macbook';
- 3 hits (POP) and 3 keywords related (2 MACBOOK, 1 APPLE found in TXT) = 6 - will be presented first
- 2 hits (POP) and 3 keywords related (1 MACBOOK, 1 APPLE found in TXT) = 5 - will be presented Second
- 2 hits (POP) and 1 keyword related (1 APPLE found in TXT) = 3 - will be presented Third
My code:
$search = isset($_GET['s']) ? htmlspecialchars($_GET['s']) : '';
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$words = explode(' ', $search);
$words_condition = array();
$arguments = array();
foreach ($words as $word) {
$words_condition[] = 'TXT LIKE ?';
$arguments[] = '%'.$word.'%';
}
$query = $db->prepare("SELECT * FROM `post` WHERE ".implode(" OR ",
$words_condition)." ORDER BY POP DESC"); $query->execute($arguments);
About my table structure ("post" table) there are 4 Column...
- ID [INT] 2. SUB (the subject of the post) [STRING] 3. TXT (the body/full text) [STRING] 4. POP [INT] (hits of visit this post).