...

View Full Version : Need help with relevant searching.



crazyeights
10-30-2009, 11:22 AM
The script below only allows me to search if the input fields match EXACTLY. However, I need the script to basically work like this:

It needs to be able to take all the fields I populate and give me the "BEST MATCH" as the top result. So say I select Blue Eyes, Brown Hair, Asian, and US as country. I need it to show me the exact matches IF ANY on top, then as you go down the line, it shows people who semi-match my query, some that are only blue eyes, brown hair, and us, and as the results go lower and lower, the fields get narrower, and narrower.

I don't know the term for this, but the code for my script's search library is below. I don't expect the script mod I need to be solved here, but I just need the term I'm referring to, and a point in the right direction. I'm getting bids to do this for 20-60 bucks on freelance sites, so that makes me assume this is an easy fix, but have no clue on how to approach it. I'm still new to PHP, about 3 months now.

I've attached the script :)

7885

kbluhm
10-30-2009, 01:40 PM
1,500+ lines of code in there. I'm kindly going to decline attempting to learn all that code to provide a solution :) ... but I would recommend looking into a full text search:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
http://devzone.zend.com/article/1304

Then you'd form your search query using the following syntax:


SELECT *, MATCH( `field1`, `field2`, `...` ) AGAINST( 'Your search terms' ) AS `score`
FROM `table_name`
WHERE MATCH( `field1`, `field2`, `...` ) AGAINST( 'Your search terms' )
ORDER BY `score` DESC

Phil Jackson
10-30-2009, 01:55 PM
ill join kbluhm with that lol

crazyeights
10-30-2009, 11:31 PM
Ya, I figured it was a bit long, but I found the QUERIES of the script which in my guess does all the searching. What can I change to make it match by percentage.




if ($profile_field['field_label'] == "uszip")
{
if (isset($_POST['uszip']))
$uszip = $DB->add_slashes(trim($_POST['uszip']));
elseif (isset($_GET['uszip']))
$uszip = $DB->add_slashes(trim($_GET['uszip']));
if (isset($_POST['dist']) && $_POST['dist'])
$dist = $DB->add_slashes(trim($_POST['dist']));
elseif (isset($_GET['dist']) && $_GET['dist'])
$dist = $DB->add_slashes(trim($_GET['dist']));
$zipfield = isset($uszip) && $uszip ? "d.data_" . $field_label : null;
}
elseif ($profile_field['field_type'] == "birthday")
{
if (isset($_POST[$profile_field['field_label'].'_from']) && $_POST[$profile_field['field_label'].'_from'])
$search_fields[] = "d.data_" . $field_label . "<=" . (date("Y") - intval($_POST[$profile_field['field_label'].'_from'])) . date("m") . date("d");
elseif (isset($_GET['age_from']) && $_GET['age_from'])
$search_fields[] = "d.data_" . $field_label . "<=" . (date("Y") - intval($_GET[$profile_field['field_label'].'_from'])) . date("m") . date("d");

if (isset($_POST[$profile_field['field_label'].'_to']) && $_POST[$profile_field['field_label'].'_to'])
$search_fields[] = "d.data_" . $field_label . ">=" . (date("Y") - intval($_POST[$profile_field['field_label'].'_to']) - 1) . date("m") . date("d");
elseif (isset($_GET[$profile_field['field_label'].'_to']) && $_GET[$profile_field['field_label'].'_to'])
$search_fields[] = "d.data_" . $field_label . ">=" . (date("Y") - intval($_GET[$profile_field['field_label'].'_to']) - 1) . date("m") . date("d");
}
elseif ($profile_field['field_type'] == "checkbox")
{
$checkboxes = array();
if (isset($_POST[$profile_field['field_label']])) {
if ( is_array($_POST[$profile_field['field_label']]) ) {
$checkboxes = ( isset($_POST[$profile_field['field_label']]) ) ? $_POST[$profile_field['field_label']] : array();
}
else {
$checkboxes = array($_POST[$profile_field['field_label']]);
}
}
elseif (isset($_GET[$profile_field['field_label']])) {
if ( is_array($_GET[$profile_field['field_label']]) ) {
$checkboxes = ( isset($_GET[$profile_field['field_label']]) ) ? $_GET[$profile_field['field_label']] : array();
}
else {
$checkboxes = array($_GET[$profile_field['field_label']]);
}
}
foreach ($checkboxes as $item_id) {
if ( is_numeric($item_id) ) {
$search_items[] = $item_id;
}
}
}
elseif ($profile_field['field_type'] == "combo" || $profile_field['field_type'] == "radio")
{
$values = array();
if (isset($_POST[$profile_field['field_label']])) {
$values = $_POST[$profile_field['field_label']];
}
elseif (isset($_GET[$profile_field['field_label']])) {
$values = $_GET[$profile_field['field_label']];
}
if ( is_array($values) && $values ) {
$search_values = array();
foreach ( $values as $value ) {
$search_values[] = $DB->add_slashes(trim($value));
}
if ( $search_values ) {
if ( count($search_values) > 1 ) {
$search_fields[] = "d.data_".$field_label." IN (".implode(",", $search_values).")";
}
else {
$search_fields[] = "d.data_" . $field_label . "=" . current($search_values);
}
}
}
elseif ($values) {
$search_fields[] = "d.data_" . $field_label . "=" . $DB->add_slashes(trim($values));
}
}
else
{
if (isset($_POST[$profile_field['field_label']]) && trim($_POST[$profile_field['field_label']]))
$search_fields[] = "d.data_" . $field_label . " LIKE '%" . $DB->add_slashes(trim($_POST[$profile_field['field_label']])) . "%'";
elseif (isset($_GET[$profile_field['field_label']]) && trim($_GET[$profile_field['field_label']]))
$search_fields[] = "d.data_" . $field_label . " LIKE '%" . $DB->add_slashes(trim($_GET[$profile_field['field_label']])) . "%'";
}
}

$j++;
}
}

crazyeights
11-01-2009, 08:40 AM
BUMP, any direction I can take. I realize the code above, but that is for full-text only.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum