You are here:  » Custom Menu with Category Hierarchy


Custom Menu with Category Hierarchy

Submitted by sirmanu on Mon, 2019-04-08 07:58 in

Hi David.

I have a relative big site using custom hierarchy (pt_categories_hierarchy has 400 rows) so I can't not display all listing. Also, I can't display them via AJAX, so it is not good for SEO.

I am trying to implement some kind of algorithm to display a mega menu and to show only a few categories based on where you are. Have you ever posted something about this in the forum?

But specially, what I am asking,is that for building this menu, I would need some function that I don't know if exists. All my names in pt_categories hierarchy are unique. So I need a function that when you pass the name (no the path), it returns the URL. Is it possible?

Example:

tapestry_buildCategoryFromName('monitors') // it returns example.com/cat/electronics/computing/monitors

Thanks!

Submitted by support on Mon, 2019-04-08 10:22

Hi,

Sure - use the following function pair to do the trick (based on how the category path is constructed from an ID in, for example admin/categories_hierarchy_configure.php)...

  function getCategoryPath($id)
  {
    global $config_databaseTablePrefix;
    $categories = array();
    do {
      $sql = "SELECT name,parent FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id = '".$id."'";
      database_querySelect($sql,$rows);
      array_unshift($categories,$rows[0]["name"]);
    } while($id = $rows[0]["parent"]);
    return implode("/",$categories);
  }
  function buildCategoryFromName($name)
  {
    global $config_databaseTablePrefix;
    $sql = "SELECT id FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE name='".database_safe($name)."'";
    if (database_querySelect($sql,$rows))
    {
      $path = getCategoryPath($rows[0]["id"]);
      return tapestry_indexHREF("category",$path);
    }
    return FALSE;
  }

And then for example;

  <a href='<?php print buildCategoryFromName("Monitors"); ?>'>Monitors</a>

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Mon, 2019-04-08 11:10

Woww! Exactly David, it is perfect. Thank you very much, it works like a charm.