Submitted by pgabriel on Mon, 2007-02-05 16:17 in Price Tapestry
Hello,
I wish to add some after search features. When a user makes a search for ex. "nokia" i wish to grab the most related categories (5) and the most related shop (5). Could this be possible?
The problem here is knowing which categories would be relevant. For example, "nokia" would be related to a "Mobile Phones" category; but there is nothing in that category name that could easily distinguish it as being related to "Nokia". Likewise with the related shop.
You might be able to asert these from the normal search results. The most related shop is likely to be the merchant of the first result; so you could access this on search.php as:
$searchresults["products"][0]["merchant"];
If you want to display this as a link to that merchant:
For categories, you would need to loop through the $searchresults array and pick out all the related categories, and then display a list of the unique ones:
Hello Gabriel,
The problem here is knowing which categories would be relevant. For example, "nokia" would be related to a "Mobile Phones" category; but there is nothing in that category name that could easily distinguish it as being related to "Nokia". Likewise with the related shop.
You might be able to asert these from the normal search results. The most related shop is likely to be the merchant of the first result; so you could access this on search.php as:
$searchresults["products"][0]["merchant"];
If you want to display this as a link to that merchant:
if ($config_useRewrite)
{
$merchantHREF = $config_baseHREF . "merchant/".tapestry_hyphenate($searchresults["products"][0]["merchant"])."/";
}
else
{
$merchantHREF = $config_baseHREF . "search.php?q=merchant:".urlencode($searchresults["products"][0]["merchant"]).":";
}
print "<a href='".$merchantHREF."'>".$searchresults["products"][0]["merchant"]."</a>";
For categories, you would need to loop through the $searchresults array and pick out all the related categories, and then display a list of the unique ones:
$categories = array();
foreach($searchresults["products"] as $v)
{
$categories[$v["category"]] = 1;
}
foreach($categories as $category => $v)
{
if ($config_useRewrite)
{
$categoryHREF = $config_baseHREF . "category/".tapestry_hyphenate($category)."/";
}
else
{
$categoryHREF = $config_baseHREF . "search.php?q=category:".urlencode($category).":";
}
print "<a href='".$categoryHREF."'>".$category."</a>";
}
This code makes use of PHP's foreach construct - see this page for more information about how this works:
http://uk.php.net/manual/en/control-structures.foreach.php
Hope this helps,
Cheers,
David.