Counting keywords in database record
| From: | Stewart Macdonald | Date: | Sun, 12 Nov 2000 05:01:42 +0000 |
| Subject: | Counting keywords in database record | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-24837@lists.php.net to get a copy of this message | ||
Hi all,
I'm designing a site that is powered by a database, with three main fields,
called "name", "details, and "description". Users enter their search term
in
a field on the search page, and then my PHP script selects all rows from the
DB where that keyword appears in any of the fields. I'd like to be able to
count the number of times the keyword appears in each row, and then sort the
search results by relevance. What is going to be the most efficient way of
doing this? Currently, I'm thinking I'll do this...
Select all the rows from the database
Copy the three fields from each row into an array
Count the number of times the keywords appear in each row, and insert this
number into a column in the array
Sort the array by the "keyword count" column
Loop through the array to display results
This seems like a horribly inefficient way of doing this, but it's the only
way I've been able to come up with. Is there a better way?
Can I use a regular expression to count the number of times the keyword
appears in each row? Or will the count_chars function work on words?
Thanks in advance,
Stewart