Jump to content

Mini php search engine, order with the search term


oz11
 Share

Recommended Posts

Hey guyz, me again.

 

Basically i wrote a mini search engine in php, the problem is that i'm not sure how to order the query so that when searching say "google news", so that those with the 'title' "Google" are at the top.

 

The code:

<style>
ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}
</style>

<form action="" method="GET">
  <label for="search">Query:</label>
  <input type="search" id="search" name="s" value="<?php echo $_GET['s']; ?>"><br>
  <input type="submit" value="Submit">
</form> 
<?php
include 'includes/config.php';

if(!empty($_GET))
{

    $search_string = $_GET['s'];
    $searchTerms = explode(' ', $search_string);
    $searchTermBits = array();
    foreach ($searchTerms as $term) {
        $term = trim($term);
        if (!empty($term)) {
            $searchTermBits[] = "keywords.word LIKE '%$term%'";
        }
    }

   print_r($searchTermBits);
   echo "<hr>";
   echo implode(' AND ', $searchTermBits);


    $query = $_GET['s'];

    $stmt = $pdo->prepare("SELECT links.title, links.URL, count(*) FROM links, link_word, keywords WHERE links.link_id = link_word.link_id AND link_word.key_id = keywords.key_id 
    AND ".implode(' OR ', $searchTermBits)." OR links.title LIKE ? GROUP BY URL");
    $stmt->execute(['%'.$query.'%']); 
    
    echo "<ul>";
    while ($row = $stmt->fetch()) {
        echo "<li><a href='".$row['URL']."'>".$row['title']."</a><li><small style='color: grey;'>".$row['URL']."</small>";
    }
    echo "</ul>";
}
?>

61351822_Screenshotfrom2022-09-1301-54-16.thumb.png.8cb4786e74671815f021838b5075f62c.png

At the moment its in second place, but i need these close matches to be first.  Help plz.

 

:thumb-up:

Link to comment
Share on other sites

Thanks. I think i'm getting there but i seem to be at a place which feels as homage as before.

 

3421094_Screenshotfrom2022-09-1317-07-05.png.3baa1ba017cc294abee66b1718db6255.png

 

<style>
    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
    }
</style>

<form action="" method="GET">
    <label for="search">Query:</label>
    <input type="search" id="search" name="s" value="<?php echo $_GET['s']; ?>"><br>
    <input type="submit" value="Submit">
</form>
<?php
include 'includes/config.php';

if (!empty($_GET)) {
    $query = $_GET['s'];
    $stmt = $pdo->prepare("SELECT title, URL, word FROM links, link_word, keywords WHERE links.link_id = link_word.link_id AND link_word.key_id = keywords.key_id AND MATCH(word) Against(?) OR MATCH(title) Against(?) GROUP BY URL");
    $stmt->execute([$query,  $query]);
    echo "<ul>";
    while ($row = $stmt->fetch()) {
        echo "<li><a href='" . $row['URL'] . "'>" . $row['title'] . "</a><li><small style='color: grey;'>" . $row['URL'] . "</small>";
    }
    echo "</ul>";
}
?>

:intoxicated: Perhaps there is an operator i could use?

Edited by oz11
code
Link to comment
Share on other sites

SELECT ... ORDER BY LOCATE('google', title) = 0

EG

mysql> select * from test;
+----------+
| title    |
+----------+
| Google   |
| Fox      |
| CNN      |
| BBC      |
| Google 2 |
+----------+
5 rows in set (0.00 sec)

mysql> select * from test order by locate('google', title) = 0, title;
+----------+
| title    |
+----------+
| Google   |
| Google 2 |
| BBC      |
| CNN      |
| Fox      |
+----------+
5 rows in set (0.02 sec)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.