You are here:  » Exact match search button

Support Forum



Exact match search button

Submitted by damir on Tue, 2008-12-30 16:38 in

Hi David,

As result of the mysql search by relevance feature there are cases where too many products are returned to the users and it make it difficult to find what they need.

I would like to add "exact match" search button next to the regular search button so the users can choose between them base on their query.

Could you please suggest how to do it?

Could you also suggest how to add sort by name?

Thanks and happy new-year,

Danny

Submitted by support on Wed, 2008-12-31 09:43

Hi Danny,

This is straight forward to add. Firstly, to add the button; in html/searchform.php, look for the following code:

<input type='submit' value='<?php print translate("Search"); ?>' />

...and REPLACE this with (i've removed the translations for clarity):

<input name='submit' type='submit' value='Search' />
<input name='submit' type='submit' value='Exact Match' />

Then, in search.php, a new section will need to be added to the normal search handler. Look for the following code on line 77:

if ($useFullText)

...and REPLACE this with

if ($_GET["SUBMIT"] == "Exact Match")
{
          $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, FROM `".$config_databaseTablePrefix."products` WHERE name = '".database_safe($parts[0])."' GROUP BY name";
          $sqlResultCount = "SELECT COUNT(DISTINCT(name)) as resultcount FROM `".$config_databaseTablePrefix."products` WHERE name = '".database_safe($parts[0])."'";
          $orderBySelection = $orderByDefault;
}
elseif($useFullText)

To add a sort by name option; first you would need to add the option to the sort SQL arrays; look for the following code beginning at line 14:

    $orderByDefault = array();
    $orderByDefault["rating"] = "rating DESC";
    $orderByDefault["priceAsc"] = "minPrice ASC";
    $orderByDefault["priceDesc"] = "minPrice DESC";
    $orderByFullText = array();
    $orderByFullText["relevance"] = "relevance DESC";
    $orderByFullText["rating"] = "rating DESC";
    $orderByFullText["priceAsc"] = "minPrice ASC";
    $orderByFullText["priceDesc"] = "minPrice DESC";

...and REPLACE this with:

    $orderByDefault = array();
    $orderByDefault["rating"] = "rating DESC";
    $orderByDefault["priceAsc"] = "minPrice ASC";
    $orderByDefault["priceDesc"] = "minPrice DESC";
    $orderByDefault["name"] = "name ASC";
    $orderByFullText = array();
    $orderByFullText["relevance"] = "relevance DESC";
    $orderByFullText["rating"] = "rating DESC";
    $orderByFullText["priceAsc"] = "minPrice ASC";
    $orderByFullText["priceDesc"] = "minPrice DESC";
    $orderByFullText["name"] = "name ASC";

Then, to add the new sort option to the banner where the user can change the sort; look for the following code on line 132:

$sortRelevance = ($sort=="relevance"?"<strong>".translate("Relevance")."</strong>":"<a href='".$sortHREF."relevance'>".translate("Relevance")."</a>");

...and then ADD the following new code on the next line:

$sortName = ", ".($sort=="name"?"<strong>".translate("Name")."</strong>":"<a href='".$sortHREF."name'>".translate("Name")."</a>");

...and finally, look for the following code on line 147:

$banner["h3"] = translate("Order by").": ".$sortRelevance.$sortRating." | ".translate("Price").": ".$sortPriceAsc.", ".$sortPriceDesc;

...and REPLACE this with:

$banner["h3"] = translate("Order by").": ".$sortRelevance.$sortName.$sortRating." | ".translate("Price").": ".$sortPriceAsc.", ".$sortPriceDesc;

Cheers,
David.