Jump to content

How to find related entry in MySQL from an array of key words?


ivytony

Recommended Posts

I'd like to find related entries in MySQL database by looking for similar terms as the current article title.

 

For example, the current article title is 'Giant Scale RC Hawker Sea Fury (HD)'

 

and after using explode for the title, the key words will become 'Giant', 'Scale', 'RC', 'Hawker', 'Sea', 'Fury', and 'HD'.

 

I want to find related entries in database that have one or more of the key words. I wonder how to do it effective?

 

Thanks

Hi

 

Explode the string containing 'Giant Scale RC Hawker Sea Fury (HD)'. Then loop though the resulting array doing a like clause for each item.

 

<?php

$wordArray = explode(" ",'Giant Scale RC Hawker Sea Fury (HD)',20);
$WhereClause = "";
foreach($wordArray as $value)
{
$WhereClause = (($WhereClause) ? " AND " : " WHERE ")." LIKE '%$value%' ";
}
$sql = "SELECT modelid from models $WhereClause ORDER BY modeldate";

?>

 

All the best

 

Keith

You may wish to consider FULLTEXT search options. Splitting the string into it's component parts and using the MATCH clause with them. See the following reference:

http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html

The one requirement is that the tables are MyISAM (until falcon sorts itself out).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.