Need Help w/ Unlimited depth categories

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Troy Lynch

    Need Help w/ Unlimited depth categories

    I'm working on writing a website which I need to have lists of
    products listed in categories and subcategories, and need to keep
    track of whats in the tree. Like how many products from the root all
    the way down through the subcategories. I was thinking maybe in each
    category it would have a field which lists all the root categories. I
    hope this is a good explanation... it's kinda hard to explain I guess.
    I figure there were probably some websites out there with methods or
    something... Any help on this would be great.... Or a direction I can
    do some researching..

    Thanks

    Troy
  • Jochen Daum

    #2
    Re: Need Help w/ Unlimited depth categories

    Hi Troy!

    On 4 Jan 2004 16:18:16 -0800, djwyldeone@yaho o.com (Troy Lynch) wrote:
    [color=blue]
    >I'm working on writing a website which I need to have lists of
    >products listed in categories and subcategories, and need to keep
    >track of whats in the tree. Like how many products from the root all
    >the way down through the subcategories. I was thinking maybe in each
    >category it would have a field which lists all the root categories. I
    >hope this is a good explanation... it's kinda hard to explain I guess.
    >I figure there were probably some websites out there with methods or
    >something... Any help on this would be great.... Or a direction I can
    >do some researching..[/color]

    There should be heaps. Have a search for "tree php adjacent" or "tree
    php nested set". These are the two most common methods.

    HTH, Jochen
    --
    Jochen Daum - CANS Ltd.
    PHP DB Edit Toolkit -- PHP scripts for building
    database editing interfaces.
    Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

    Comment

    • Joaquim Amado Lopes

      #3
      Re: Need Help w/ Unlimited depth categories

      Greetings.

      One possibility is to have a field like "mother_categor y" in the
      categories list. Each category would have a "mother", referred by it's
      record ID or something like that. The category with mother_category =0
      would be the root.

      To count the products in a branch, create a recursive function that
      adds the produtcs in every sub-category of the category you want to
      start.

      Something like:
      Table categories:
      id
      category
      mother_category <- categories.id

      Table products:
      id
      cat_id <- categories.id
      product

      The function:
      Parameter: top_id (id of the top category you want to consider)
      How it works:
      count the number of products with cat_id = top_id;
      get the list of the sub-categories of the top_id category;
      for each sub-category, it calls itself and adds the result to the
      sum of products
      Returns: the sum of products of that branch

      Hope this helps,
      Joaquim Amado Lopes

      Comment

      Working...