Hi David,
I include the following code into html/footer.php it works fine. But when i put these code on html/header.php i have this error.
1. In the search box it shows
SELECT DISTINCT(category) as category FROM `products` ORDER BY RAND() LIMIT 5
When i click into the product page it shows this error.
Price: USD$ from
Warning: Invalid argument supplied for foreach() in /............../html/product.php on line 26
How can i fix this.
2. Is it possible to include random merchant as well since i hv random category n random brand.
thanks
jack
<?php
// 5 random brands
print "<p>";
$q = sprintf("SELECT DISTINCT(brand) as brand FROM `".$config_databaseTablePrefix."products`
ORDER BY RAND() LIMIT 5");
if (database_querySelect($q,$rows))
{
foreach($rows as $product)
{
if ($config_useRewrite)
{
$href = $config_baseHREF."brand/".tapestry_hyphenate($product["brand"])."/";
}
else
{
$href = $config_baseHREF."search.php?q=brand:".urlencode($product["brand"]).":";
}
print "<a href='".$href."'>".$product["brand"]."</a> ";
}
}
print "</p>";
// 5 random categories
print "<p>";
$q = sprintf("SELECT DISTINCT(category) as category FROM `".$config_databaseTablePrefix."products`
ORDER BY RAND() LIMIT 5");
if (database_querySelect($q,$rows))
{
foreach($rows as $product)
{
if ($config_useRewrite)
{
$href = $config_baseHREF."category/".tapestry_hyphenate($product["category"])."/";
}
else
{
$href = $config_baseHREF."search.php?q=category:".urlencode($product["category"]).":";
}
print "<a href='".$href."'>".$product["category"]."</a> ";
}
}
print "</p>";
?>
Hi David,
Now the i works. but when i click on the products still can see these error code.
Price: USD$ from
Warning: Invalid argument supplied for foreach() in /home/..........html/product.php on line 26
On line 26 html/product.php is as follows.
<?php
foreach($product["products"] as $priceProduct):
?>
thanks
Hi,
Sorry - I should have spotted that - it's the same problem as before but this time overwriting the $product variable. In your code, you have 2 or times the following construct:
foreach($rows as $product)
You will need to change this, and use, for example:
foreach($rows as $tempProduct)
Perhaps the easiest thing to do is open html/header.php and do a SEARCH AND REPLACE with your text editor, replacing $product with $tempProduct
Hope this helps!
Cheers,
David.
Hi David,
Now is ok but another problem arises, instead of 5 randoms it show 1 only and repeated 5 times. Is it got to do with the mod where i eliminate the title in search box ie. "merchant:kelkoo" "category:kelkoo" "brand:kelkoo" to just "kelkoo"
regards
jack
Hi Jack,
That would indicate that you haven't changed all instances of $product. Don't forget that as well as the version in the foreach() statement, you have to change the code in the loop below because it is no longer using the $product variable. I think you are seeing the same merchant 5 times because it is using the $product variable that has been prepared for the product page!
Make sure that $product on its own is nowhere in html/header.php...
Cheers,
David.
Hi David,
Thank you very much. It works now.
Cheers,
Jack.
Here is the full code.
<?php
// 5 random brands
print "<p>";
$sql = sprintf("SELECT DISTINCT(brand) as brand FROM `".$config_databaseTablePrefix."products`
ORDER BY RAND() LIMIT 5");
if (database_querySelect($sql,$rows))
{
foreach($rows as $tempProduct)
{
if ($config_useRewrite)
{
$href = $config_baseHREF."brand/".tapestry_hyphenate($tempProduct["brand"])."/";
}
else
{
$href = $config_baseHREF."search.php?q=brand:".urlencode($tempProduct["brand"]).":";
}
print "<a href='".$href."'>".$tempProduct["brand"]."</a> ";
}
}
print "</p>";
// 5 random categories
print "<p>";
$sql = sprintf("SELECT DISTINCT(category) as category FROM `".$config_databaseTablePrefix."products`
ORDER BY RAND() LIMIT 5");
if (database_querySelect($sql,$rows))
{
foreach($rows as $tempProduct)
{
if ($config_useRewrite)
{
$href = $config_baseHREF."category/".tapestry_hyphenate($tempProduct["category"])."/";
}
else
{
$href = $config_baseHREF."search.php?q=category:".urlencode($tempProduct["category"]).":";
}
print "<a href='".$href."'>".$tempProduct["category"]."</a> ";
}
}
print "</p>";
// 5 random merchants
$sql = sprintf("SELECT merchant FROM `".$config_databaseTablePrefix."feeds`
ORDER BY RAND() LIMIT 5");
if (database_querySelect($sql,$rows))
{
foreach($rows as $tempProduct)
{
if ($config_useRewrite)
{
$href = $config_baseHREF."merchant/".tapestry_hyphenate($tempProduct["merchant"])."/";
}
else
{
$href = $config_baseHREF."search.php?q=merchant:".urlencode($tempProduct["merchant"]).":";
}
print "<a href='".$href."'>".$tempProduct["merchant"]."</a> ";
}
}
print "</p>";
?>
Hello Jack,
The SQL is showing up in the search box because you are using the variable $q, which is the same as the variable the query is written into. Therefore, you will need to use a different variable name. I would recommend $sql or something like that so that it doesn't get confused with any other variables.
To select a random merchant, you can do basically the same as you have for the category and brand, but using the following SQL (don't forget the point above about changing the variable name, but i'll keep it with $q for now)....
// 5 random merchants
$q = sprintf("SELECT merchant FROM `".$config_databaseTablePrefix."feeds`
ORDER BY RAND() LIMIT 5");
That should do the trick!
Cheers,
David.