Category and sub-category logic

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

  1. #1

    Default 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 category "Auto" there would be "cars","bikes" etc
    under "banking" there would be "financing","loans" etc (I as admin create
    master and sub-categories)

    then if you click on "cars" you would either see a sub category or all the
    listings there

    I have never done a project like this so am a bit confused but I am pretty
    sure quite a few of you must have done this because its a bit common on the
    net and while I am kind of new to php, most of you guys are fossils :-D

    So far the logic I have worked out is:
    Create a "master_category" table for the main categories, a "child_table"
    for the subs, a "the_listings" table for the details which will have a
    reference (number or word) field which will be to keep a reference as to
    which category/sub-category it belongs to..

    Tell me if my logic is wrong or I missed anything

    The part where I am confused is, on the front page (say index.php) how do I
    display all the categories and sub categories in the correct order?

    eg:

    Auto
    --Cars
    --Bikes

    Banking
    --Finances
    --Loans

    Women
    --Boring but REALLY good looking
    --Great fun but not-so-good-looking


    Any ideas? I searched hotscripts and google but found crappy programs like
    phpyellow and zclassifieds which are really no good to me. If you know of
    something that already does the above kindly share it with me.

    Thanks,
    -Ryan

    -------------------
    -----------------
    The government announced today that it is changing it's emblem to a condom
    because it more clearly reflects the government's political stance. A condom
    stands up to inflation, halts production, destroys the next generation,
    protects a bunch of pricks, and gives you a sense of security while you're
    actually getting screwed.
    -------------------
    -----------------

    Ryan A 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. [PHP] Category and sub-category logic
      Ryan A <mailto:ryan@jumac.com> on Thursday, August 14, 2003 3:21 PM said: Read this http://www.sitepoint.com/article/1105 and you will know what...
  3. #2

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

    On Fri, 15 Aug 2003 00:20:58 +0200, you wrote:
    >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 category "Auto" there would be "cars","bikes" etc
    >under "banking" there would be "financing","loans" etc (I as admin create
    >master and sub-categories)
    >So far the logic I have worked out is:
    >Create a "master_category" table for the main categories, a "child_table"
    >for the subs, a "the_listings" table for the details which will have a
    >reference (number or word) field which will be to keep a reference as to
    >which category/sub-category it belongs to..
    >
    >Tell me if my logic is wrong or I missed anything
    >
    >The part where I am confused is, on the front page (say index.php) how do I
    >display all the categories and sub categories in the correct order?
    You need a pig's ear on the database table. It's really simple once you've
    seen it done.

    Basically, each row in the database contains a reference to its parent's
    primary key.

    Call our table "category".

    categoryid parentid title
    1 0 Auto
    2 1 Cars
    3 1 Bikes
    4 0 Banking
    5 4 Finances
    6 4 Loans

    SELECT title FROM category WHERE parentid = 0

    gives us the root elements in the tree (Auto, Banking) while

    SELECT title FROM category WHERE parentid = 4

    gives us the children of Banking (Finances, Loans).

    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).

    The fun thing about this structure is writing the delete function.

    David Otton 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