Hello
I'm wondering if you could explain how I can add a random product to appear under the search bar on my website.
Cheers
Mally
Hi Mal,
Is it only the image that overlaps?
What I would suggest doing then, is instead of using the stock html/featured.php HTML module to display the random product, make a copy of it as something else, for example html/random.php. Then, in the modifications described above, instead of:
if (isset($featured)) require("html/featured.php");
...change this to:
if (isset($featured)) require("html/random.php");
Then, you can start editing the HTML in html/random.php in order to make it fit in with your design. You will notice the image width (currently 80) on line 11 of the original file.
Hope this helps!
Cheers,
David.
Mally,
Get in touch via the Price Tapestry Templates Forum if you need a hand customising your template :-)
Dave
Hi David,
I have added this code above using a new random.php file, having some issues with mod rewrite.
If I am in my top level domain www.domain.com.au it works fine howevewr if im in www.domain.com.au/product the link becomes www.domain.com.au/product/product/tennis-rackets.html or
www.domain.com.au/merchants/product/tennis-rackets.html
I have also ended up with http://www.domain.com.au/search.php?q=producttennis+rackets
my code in footer is as follows
Random Product
<?php
$sql = "SELECT name FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 1";
if (database_querySelect($sql,$rows))
{
$sqlNames = array();
foreach($rows as $featured)
{
$sqlNames[] = "'".$featured["name"]."'";
}
$sqlIn = implode(",",$sqlNames);
$sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";
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/random.php");
unset($featured); // make sure it doesn't interfere with the main featured products!
?>
and random.php is as follows
<?php foreach($featured["products"] as $product): ?>
<h3 class="featuredh"><a href='<?php print $product["productHREF"]; ?>' title='<?php print $product["name"]; ?>'><?php print $product["name"]; ?></a></h3><br>
<?php if ($product["image_url"]): ?>
<a href='<?php print $product["productHREF"]; ?>' title='<?php print $product["name"]; ?>'><img width='80' height='80' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["name"]; ?>' title='<?php print $product["name"]; ?>' /></a><br><?php print substr($product["description"],0,100); ?>...<br>
<?php endif; ?>
<?php if ($config_useInteraction): ?>
<small>
<?php if ($product["reviews"]): ?>
<?php print tapestry_stars($product["rating"],"s"); ?>
<a href='<?php print $product["reviewHREF"]; ?>' title='<?php print $product["reviews"]." ".translate("Reviews"); ?>'><?php print $product["reviews"]." ".translate("Reviews"); ?></a>
<?php else: ?>
<br>
<?php endif; ?>
</small>
<?php endif; ?>
<p>
<?php if ($product["numMerchants"] > 1): ?>
<em><?php print translate("from"); ?></em> <strong><?php print $config_currencyHTML.$product["minPrice"]; ?></strong><br />
<span class='nobr'><a href='<?php print $product["productHREF"]; ?>' title='<?php print translate("Compare Prices"); ?>'><?php print translate("Compare Prices"); ?></a></span>
<?php else: ?>
<strong><?php print $config_currencyHTML.$product["price"]; ?></strong><br />
<span class='nobr'><a href='<?php print $product["productHREF"]; ?>' title='<?php print translate("More Information"); ?>'><?php print translate("More Information"); ?></a></span>
<?php endif; ?>
</p>
<?php endforeach; ?>
<div class="clear"></div>
Any ideas
Richard
Hi Richard,
The easiest thing to do is to modify this section of the code in your footer:
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"]);
}
...as follows:
if ($config_useRewrite)
{
$featured["products"][$k]["productHREF"] = $config_baseHREF."product/".tapestry_hyphenate($product["name"]).".html";
$featured["products"][$k]["reviewHREF"] = $config_baseHREF."review/".tapestry_hyphenate($product["name"]).".html";
}
else
{
$featured["products"][$k]["productHREF"] = $config_baseHREF."products.php?q=".urlencode($product["name"]);
$featured["products"][$k]["reviewHREF"] = $config_baseHREF."reviews.php?q=".urlencode($product["name"]);
}
Prefixing each link with $config_baseHREF will mean the links will work from any location.
Cheers,
David.
Hi Mal,
The easiest way to do it is to crib the "Featured Products" code from index.php, and modify it slightly to pick one product from the database at random, then do the price comparison query on that product, then include the html/featured.php HTML module. Try adding the following code beneath your code for the left-hand search box....
<?php
$sql = "SELECT name FROM `".$config_databaseTablePrefix."products` ORDER BY RAND() LIMIT 1";
if (database_querySelect($sql,$rows))
{
$sqlNames = array();
foreach($rows as $featured)
{
$sqlNames[] = "'".$featured["name"]."'";
}
$sqlIn = implode(",",$sqlNames);
$sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$sqlIn.") GROUP BY name";
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");
unset($featured); // make sure it doesn't interfere with the main featured products!
?>
Hope this helps!
Cheers,
David.