[PHP] Category and sub-category logic

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default RE: [PHP] Category and sub-category logic

    Ryan A <mailto:ryan@jumac.com>
    on Thursday, August 14, 2003 3:21 PM said:

    Read this [url]http://www.sitepoint.com/article/1105[/url] and you will know what
    you should do.

    The database organization you want to use is called Modified Preorder
    Tree Traversal. It takes only one table to create all the relationships
    needed.

    If you have any questions about this technique send me an email as I've
    got some experience with this method.


    Chris.

    p.s. The easiest way (I think) to learn this method is to actually use
    your own data and draw it out on a piece of paper (as opposed to trying
    to visualize the map in your head).
    Chris W. Parker Guest

  2. Similar Questions and Discussions

    1. Many-to-Many with Sub Category
      I have two linking tables, one for my main categories and one for sub categories with the many-to-many insert wizard applied to each. Works great on...
    2. Add Category in Site Definition
      Is there a way to add a new Category in the "Advanced" section of the Site Definition? I have written an extension and would like to have...
    3. select any data from category
      Hi everyone - in the following code i want to allow the user t choose 'any'. Example choose the area - central language - any...
    4. Verity and Category
      I have a problem using category with Verity. My search form allows people to search in a collection of laws combining full-text searching and...
    5. Category and sub-category logic
      Hi, I am thinking of making a program for personal use which does a very simple thing, just displays listings under categories, eg: under main...
  3. #2

    Default RE: [PHP] Category and sub-category logic

    David Otton <mailto:phpmail@jawbone.freeserve.co.uk>
    on Thursday, August 14, 2003 4:58 PM said:
    > The advantage of doing it this way is that your tree structure is
    > generic and can have many levels. The disadvantage is that you may
    > need many SQL queries to fully traverse the tree (though people
    > rarely want to do this, and sub-selects, clever joins or post-query
    > processing can reduce the overhead).
    Which is why you should use the Modified Preorder Tree Traversal method
    instead. With that method you've got one table that defines the
    categories AND their relationships to each other.

    Your SQL queries end up looking something like this.

    SELECT name
    FROM categories
    WHERE lft >= x
    AND rgt <= y



    Chris.
    Chris W. Parker Guest

  4. #3

    Default Re: [PHP] Category and sub-category logic

    From: "Mark" <mark_weinstock@yahoo.com>

    > I'm trying to get a handle on this algorithm as well. If you add
    > additional children to a parent record, doesn't that require
    > renumbering all the rgt and lft values in the lineage? For example
    > (table shamelessly stolen from a website):
    >
    > Personnel
    > emp lft rgt
    > ======================
    > 'Albert' 1 12
    > 'Bert' 2 3
    > 'Chuck' 4 11
    > 'Donna' 5 6
    > 'Eddie' 7 8
    > 'Fred' 9 10
    >
    > If I want to add 'Joe' as a subordinate to 'Fred', doesn't that mean
    > I have to change the lft values for Fred, Chuck, and Albert? Yes,
    > deleting definitely seems easier. I'm curious about adding...
    Yes, you will have to update all of the right and left numbers to the right
    (or higher) than the node where you are inserting. Deleting is basically the
    same method done in reverse, but can get complicated when you delete a node
    and have to determine what to do with the children.

    I'd recommend, if you're really interested in this, to do some searching on
    nested set tutorials. You could specifically search for Joe Celko who has
    written a lot of information on the topic.

    While it seems like a big deal to have to update all of the numbers, it's
    really not. It can be done with a single query (with subselects). Yes, it's
    more costly than inserting a name into an adjacentcy list model, but it's
    worth it for all that you gain from the nested set model.

    ---John Holmes...

    Cpt John W. Holmes Guest

  5. #4

    Default Re: [PHP] Category and sub-category logic

    Wouldnąt it just be easier to do a relationship table as in

    Personnel
    emp ID parent
    ========================
    'Albert' 1 0
    'Bert' 2 1
    'Chuck' 3 1
    'Donna' 4 0
    'Eddie' 5 4
    'Fred' 6 0
    'Joe' 7 6

    This way you know the ID of the person and the parentID if they have one,
    otherwise they are a 0 which means they are the parent.. Seems like this
    would be a much easier way of doing it doesnąt it? And only one SQL
    query...

    Rick

    "The only way to have a friend is to be one." - Ralph Waldo Emerson
    > From: Mark <mark_weinstock@yahoo.com>
    > Date: Fri, 15 Aug 2003 07:18:41 -0700 (PDT)
    > To: "Chris W. Parker" <cparker@swatgear.com>
    > Cc: [email]php-general@lists.php.net[/email]
    > Subject: RE: [PHP] Category and sub-category logic
    >
    > I'm trying to get a handle on this algorithm as well. If you add
    > additional children to a parent record, doesn't that require
    > renumbering all the rgt and lft values in the lineage? For example
    > (table shamelessly stolen from a website):
    >
    > Personnel
    > emp lft rgt
    > ======================
    > 'Albert' 1 12
    > 'Bert' 2 3
    > 'Chuck' 4 11
    > 'Donna' 5 6
    > 'Eddie' 7 8
    > 'Fred' 9 10
    >
    > If I want to add 'Joe' as a subordinate to 'Fred', doesn't that mean
    > I have to change the lft values for Fred, Chuck, and Albert? Yes,
    > deleting definitely seems easier. I'm curious about adding...
    >
    > Mark
    >
    >
    >
    > --- "Chris W. Parker" <cparker@swatgear.com> wrote:
    >> David Otton <mailto:phpmail@jawbone.freeserve.co.uk>
    >> on Thursday, August 14, 2003 4:58 PM said:
    >>
    >>> The advantage of doing it this way is that your tree structure is
    >>> generic and can have many levels. The disadvantage is that you
    >> may
    >>> need many SQL queries to fully traverse the tree (though people
    >>> rarely want to do this, and sub-selects, clever joins or
    >> post-query
    >>> processing can reduce the overhead).
    >>
    >> Which is why you should use the Modified Preorder Tree Traversal
    >> method
    >> instead. With that method you've got one table that defines the
    >> categories AND their relationships to each other.
    >>
    >> Your SQL queries end up looking something like this.
    >>
    >> SELECT name
    >> FROM categories
    >> WHERE lft >= x
    >> AND rgt <= y
    >>
    >>
    >>
    >> Chris.
    >>
    >> --
    >> PHP General Mailing List (http://www.php.net/)
    >> To unsubscribe, visit: http://www.php.net/unsub.php
    >>
    >
    >
    > =====
    > Mark Weinstock
    > mark_weinstock@yahoo.com
    > ***************************************
    > You can't demand something as a "right" unless you are willing to fight to
    > death to defend everyone else's right to the same thing.
    > ***************************************
    >
    > __________________________________
    > Do you Yahoo!?
    > Yahoo! SiteBuilder - Free, easy-to-use web site design software
    > http://sitebuilder.yahoo.com
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    Richard Baskett Guest

  6. #5

    Default Re: [PHP] Category and sub-category logic

    --- "CPT John W. Holmes" <holmes072000@charter.net> wrote:
    > From: "Mark" <mark_weinstock@yahoo.com>
    >
    >
    > > I'm trying to get a handle on this algorithm as well. If you add
    > > additional children to a parent record, doesn't that require
    > > renumbering all the rgt and lft values in the lineage? For
    > example
    > > (table shamelessly stolen from a website):
    > >
    > > Personnel
    > > emp lft rgt
    > > ======================
    > > 'Albert' 1 12
    > > 'Bert' 2 3
    > > 'Chuck' 4 11
    > > 'Donna' 5 6
    > > 'Eddie' 7 8
    > > 'Fred' 9 10
    > >
    > > If I want to add 'Joe' as a subordinate to 'Fred', doesn't that
    > mean
    > > I have to change the lft values for Fred, Chuck, and Albert? Yes,
    > > deleting definitely seems easier. I'm curious about adding...
    >
    > Yes, you will have to update all of the right and left numbers to
    > the right
    > (or higher) than the node where you are inserting. Deleting is
    > basically the
    > same method done in reverse, but can get complicated when you
    > delete a node
    > and have to determine what to do with the children.
    >
    > I'd recommend, if you're really interested in this, to do some
    > searching on
    > nested set tutorials. You could specifically search for Joe Celko
    > who has
    > written a lot of information on the topic.
    Thanks. I'll take a look. I'm considering changing to this algorithm
    from my basic parent-ID table.
    >
    > While it seems like a big deal to have to update all of the
    > numbers, it's
    > really not. It can be done with a single query (with subselects).
    > Yes, it's
    > more costly than inserting a name into an adjacentcy list model,
    > but it's
    > worth it for all that you gain from the nested set model.
    >
    > ---John Holmes...
    >
    >
    > --
    > PHP General Mailing List ([url]http://www.php.net/[/url])
    > To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    >

    =====
    Mark Weinstock
    [email]mark_weinstock@yahoo.com[/email]
    ***************************************
    You can't demand something as a "right" unless you are willing to fight to death to defend everyone else's right to the same thing.
    ***************************************

    __________________________________
    Do you Yahoo!?
    Yahoo! SiteBuilder - Free, easy-to-use web site design software
    [url]http://sitebuilder.yahoo.com[/url]
    Mark Guest

  7. #6

    Default Re: [PHP] Category and sub-category logic

    From: "Richard Baskett" <php@baskettcase.com>

    > Wouldnąt it just be easier to do a relationship table as in
    >
    > Personnel
    > emp ID parent
    > ========================
    > 'Albert' 1 0
    > 'Bert' 2 1
    > 'Chuck' 3 1
    > 'Donna' 4 0
    > 'Eddie' 5 4
    > 'Fred' 6 0
    > 'Joe' 7 6
    >
    > This way you know the ID of the person and the parentID if they have one,
    > otherwise they are a 0 which means they are the parent.. Seems like this
    > would be a much easier way of doing it doesnąt it? And only one SQL
    > query...
    Possibly. It depends on what you're trying to model. We're getting way off
    topic for PHP, though.

    What you gave above is called an adjacency list model and the other example
    given was a nested set (or tree) model. Each has it's advantages and
    disadvantages. I recommend counsulting your good friend Google if you're
    interested in the details. :)

    ---John Holmes...

    Cpt John W. Holmes Guest

  8. #7

    Default Chris->Re: [PHP] Category and sub-category logic

    Hey,
    Sorry I didnt reply, was having some computer problems.
    Thanks for the link and the advise, will look into it.
    Cheers,
    -Ryan


    We will slaughter you all! - The Iraqi (Dis)information ministers site
    [url]http://MrSahaf.com[/url]


    ----- Original Message -----
    From: "Chris W. Parker" <cparker@swatgear.com>
    To: "David Otton" <phpmail@jawbone.freeserve.co.uk>; "Ryan A"
    <ryan@jumac.com>
    Cc: <php-general@lists.php.net>
    Sent: Friday, August 15, 2003 2:04 AM
    Subject: RE: [PHP] Category and sub-category logic

    > David Otton <mailto:phpmail@jawbone.freeserve.co.uk>
    > on Thursday, August 14, 2003 4:58 PM said:
    >
    > > The advantage of doing it this way is that your tree structure is
    > > generic and can have many levels. The disadvantage is that you may
    > > need many SQL queries to fully traverse the tree (though people
    > > rarely want to do this, and sub-selects, clever joins or post-query
    > > processing can reduce the overhead).
    >
    > Which is why you should use the Modified Preorder Tree Traversal method
    > instead. With that method you've got one table that defines the
    > categories AND their relationships to each other.
    >
    > Your SQL queries end up looking something like this.
    >
    > SELECT name
    > FROM categories
    > WHERE lft >= x
    > AND rgt <= y
    >
    >
    >
    > Chris.
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    >
    Ryan A Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139