You are here:  » Categories

Support Forum



Categories

Submitted by Halion on Wed, 2013-02-06 12:46 in

Hi David,

Is there a way to show only categories or brands in the navigation sidebar which have more than say, 10 products in the db? Otherwise the sidebar can get loaded with far too many categories with only 1 or 2 products. Or is there a bandwidth problem to do this?

<?php
  $sql 
"SELECT DISTINCT(brand) FROM `".$config_databaseTablePrefix."products` WHERE brand <> '' LIMIT 20";
  
database_querySelect($sql,$rows);
  print 
"<p>";
  foreach(
$rows as $row)
  {
    if (
$config_useRewrite)
    {
      
$href $config_baseHREF."brand/".tapestry_hyphenate($row["brand"])."/";
    }
    else
    {
      
$href $config_baseHREF."search.php?q=brand:".urlencode($row["brand"]);
    }
    print 
" <a href='".$href."'>".$row["brand"]."</a> "; print "<br />";
  }
  print 
"</p>";
?>

Thanks
Mike

Submitted by support on Wed, 2013-02-06 12:59

Hi Mike,

Sure - have a go with something like this; (for brands as per the code in your post)

<?php
  $sql 
"SELECT brand,COUNT(id) as numProducts FROM `".$config_databaseTablePrefix."products` WHERE brand <> '' GROUP BY brand ORDER BY numProducts DESC LIMIT 10";
  
database_querySelect($sql,$rows);
  
// resort by alpha
  
function brandCompare($a,$b)
  {
    return 
strcmp($a["brand"],$b["brand"]);
  }
  
usort($rows,"brandCompare");
  print 
"<p>";
  foreach(
$rows as $row)
  {
    if (
$config_useRewrite)
    {
      
$href $config_baseHREF."brand/".tapestry_hyphenate($row["brand"])."/";
    }
    else
    {
      
$href $config_baseHREF."search.php?q=brand:".urlencode($row["brand"]);
    }
    print 
" <a href='".$href."'>".$row["brand"]."</a> "; print "<br />";
  }
  print 
"</p>";
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by Halion on Wed, 2013-02-06 13:14

Hi David, and thanks for your quick response. Unfortunately the code you suggested doesn't seem to work correctly, only displays a few categories and ignores other categories with lots of products. I'll continue testing here, thanks again.

Submitted by support on Wed, 2013-02-06 13:19

Hi Mike,

Category version should be as follows; let me know if still not returning as expected...

<?php
  $sql 
"SELECT category,COUNT(id) as numProducts FROM `".$config_databaseTablePrefix."products` WHERE category <> '' GROUP BY category ORDER BY numProducts DESC LIMIT 10";
  
database_querySelect($sql,$rows);
  
// resort by alpha
  
function categoryCompare($a,$b)
  {
    return 
strcmp($a["category"],$b["category"]);
  }
  
usort($rows,"categoryCompare");
  print 
"<p>";
  foreach(
$rows as $row)
  {
    if (
$config_useRewrite)
    {
      
$href $config_baseHREF."category/".tapestry_hyphenate($row["category"])."/";
    }
    else
    {
      
$href $config_baseHREF."search.php?q=category:".urlencode($row["category"]);
    }
    print 
" <a href='".$href."'>".$row["category"]."</a> "; print "<br />";
  }
  print 
"</p>";
?>

Cheers,
David.
--
PriceTapestry.com