Jump to content

Recommended Posts

Hi everyone

 

Im pretty new to this so bear with me if i state the obvious or anything :)

 

Im currently adding a search function to my website using mysql and php.

 

First of all i actually have the search working using the like function and the % wildcards to bring back multiple results.

 

For searching i am looking at two coloums in my database title and key words. The keywords are broken up by spaces.

 

The issue im having is when i search if i search for example new york this will bring back all the entries with new your in them but if i type new york city i get none because none of my keywords say city.

 

Same happens if the words are types out of sequence from the way they are in the database.

 

Does anyone have any idea how i can build this so for example with the new york city one the database will still return any entries that has new york?

 

 

Hope this makes sense and i hope you can help.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/271207-html-php-help/
Share on other sites

Here is a copy of the code i am using.

 

the database structure is simply

 

ID

Title

Keywords

 

<?php

$search = $_GET['search'];

$maxRows_Recordset1 = 10;

$pageNum_Recordset1 = 0;

if (isset($_GET['pageNum_Recordset1'])) {

$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];

}

$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

 

mysql_select_db($database_Laughing_seagull, $Laughing_seagull);

$query_Recordset1 = "SELECT products.Title FROM products WHERE products.keywords LIKE '%".$search."%' OR products.Title LIKE '%".$search."%'";

$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);

$Recordset1 = mysql_query($query_limit_Recordset1, $Laughing_seagull) or die(mysql_error());

$row_Recordset1 = mysql_fetch_assoc($Recordset1);

 

if (isset($_GET['totalRows_Recordset1'])) {

$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];

} else {

$all_Recordset1 = mysql_query($query_Recordset1);

$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);

}

$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

?>

Link to comment
https://forums.phpfreaks.com/topic/271207-html-php-help/#findComment-1395278
Share on other sites

Consider full-text search.

 

Depending on the size of your table now, or in the future, using wildcards can be terrible on performance. And while full-text searching is not the creme de la creme of table search, it does a better job, hands down.

Edited by mrMarcus
Link to comment
https://forums.phpfreaks.com/topic/271207-html-php-help/#findComment-1395286
Share on other sites

First thing that comes to mind is to split the search term that the user types on the space character. Then, search the database for each of the terms separately. So instead of searching for "new york city" you instead search for "new", "york", and "city". From this you can eliminate duplicates and possibly rank them based on how many times each word shows up. Maybe require at least 50% of the words to match to cut out words like "in" or "the" from flooding the results.

 

Hopefully this is helpful, it actually gave me an idea for my own search feature, so thank you.

Link to comment
https://forums.phpfreaks.com/topic/271207-html-php-help/#findComment-1395287
Share on other sites

First thing that comes to mind is to split the search term that the user types on the space character. Then, search the database for each of the terms separately. So instead of searching for "new york city" you instead search for "new", "york", and "city". From this you can eliminate duplicates and possibly rank them based on how many times each word shows up. Maybe require at least 50% of the words to match to cut out words like "in" or "the" from flooding the results.

 

Hopefully this is helpful, it actually gave me an idea for my own search feature, so thank you.

 

Ah it was nothing:)

 

Is this the full text searching that mr marcus described or something else?

 

If so how would i go about splitting the search term up?

Link to comment
https://forums.phpfreaks.com/topic/271207-html-php-help/#findComment-1395291
Share on other sites

Ah it was nothing:)

 

Is this the full text searching that mr marcus described or something else?

 

If so how would i go about splitting the search term up?

First thing that comes to mind is to split the search term that the user types on the space character. Then, search the database for each of the terms separately. So instead of searching for "new york city" you instead search for "new", "york", and "city". From this you can eliminate duplicates and possibly rank them based on how many times each word shows up. Maybe require at least 50% of the words to match to cut out words like "in" or "the" from flooding the results.

 

Hopefully this is helpful, it actually gave me an idea for my own search feature, so thank you.

 

I hope you're not referring to cycling through the table 3 separate times for every one search.

 

Full-text search has Stop Words to handle words like "in" and "the".

 

It also allows for Boolean search.

Edited by mrMarcus
Link to comment
https://forums.phpfreaks.com/topic/271207-html-php-help/#findComment-1395296
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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