You are here:  » Random Brand & Category

Support Forum



Random Brand & Category

Submitted by multiz on Wed, 2007-05-09 23:01 in

Hello,

It's been a long time since I've worked on this, but how would it be possible to put 5 random brands and categories on each product page -- using the footer file?

Thanks for any tips,

Michael

ZZPrices; Electronics Price Comparison Shopping

Submitted by support on Thu, 2007-05-10 14:28

Hi Michael,

That's reasonably straight forward to do - here's some code to get you started:

<?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>&nbsp;";
    }
  }
  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>&nbsp;";
    }
  }
  print 
"</p>";
?>

Just add that to html/footer.php and away you go. You might want to modify the way it generates the HTML of course, but that's the framework you need...

Cheers,
David.

Submitted by multiz on Thu, 2007-05-10 16:21