Hi,
I'm looking to create a page that will display featured products which fall within a specific category.
For example, let's say I add 3 kids toys and 3 teenagers toys to the featured products, can I show just the kids toys featured products when someone selects the kids toys category?
Hi David,
again, thanks for your superspeed response!
I would like to show all featured on the homepage still, and then when browsing by category, show the featured products in that category.
I haven't added any code yet...
Cheers
Jamie
Hi Jamie,
OK, first stage to is to extract the Featured Products code from index.php and move it out into a separate file that can be called by both index.php and search.php in the case of a category: search.
The Featured Products code is actually the bulk of index.php, as follows:
$sql = "SELECT * FROM `".$config_databaseTablePrefix."featured` ORDER BY sequence";
if (database_querySelect($sql,$rows))
{
$sqlNames = array();
$sqlCase = "CASE name";
foreach($rows as $featured)
{
$sqlNames[] = "'".$featured["name"]."'";
$sqlCase .= " WHEN '".database_safe($featured["name"])."' THEN ".$featured["sequence"];
}
$sqlCase .= " END AS sequence";
$sqlIn = implode(",",$sqlNames);
$sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, ".$sqlCase." FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name ORDER BY sequence";
database_querySelect($sql,$rows);
$featured["products"] = $rows;
foreach($featured["products"] as $k => $product)
{
if ($config_useRewrite)
{
$featured["products"][$k]["productHREF"] =
"product/".tapestry_hyphenate($product["name"]).".html";
$featured["products"][$k]["reviewHREF"] =
"review/".tapestry_hyphenate($product["name"]).".html";
}
else
{
$featured["products"][$k]["productHREF"] =
"products.php?q=".urlencode($product["name"]);
$featured["products"][$k]["reviewHREF"] =
"reviews.php?q=".urlencode($product["name"]);
}
}
}
if (isset($featured)) require("html/featured.php");
CUT the above code and PASTE it into a new file called featuredCommon.php, and in it's place in index.php use:
require("featuredCommon.php");
Now edit the new featuredCommon.php file to check for and use the category component of the search query. To do this, REPLACE the following line:
$sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, ".$sqlCase." FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name ORDER BY sequence";
...with this new code:
if ($parts[0] == "category"])
{
$sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, ".$sqlCase." FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") AND category='".database_safe($parts[1])."' GROUP BY name ORDER BY sequence";
}
else
{
$sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, ".$sqlCase." FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name ORDER BY sequence";
}
Finally, to call the new featuredCommon.php from search.php, look towards the end of the file for the following line:
require("html/header.php");
...and INSERT anywhere after that point (but before html/footer.php) depending on where you want the featured products to be displayed relative to the other components of the page, the following code:
if ($parts[0]=="category")
{
require("featuredCommon.php");
}
Hope this helps!
Cheers,
David.
Hi Jamie,
Just to clarify; do you still want to have all featured products on the home page; and then when browsing by category you want to show those featured products in the currently selected category?
If so; have you already added code to show all featured products on the category page or would that be the place to start and then get that filtered down by the category being browsed?
Cheers,
David.