Skip to main content
Inspiring
December 3, 2008
Question

category and sub category

  • December 3, 2008
  • 3 replies
  • 599 views
Hi I have table where i want to display the categories with parent id 0 and When parent id is 1 or 2 and so on...

it should display as :

autos/auto1
autos/cars

Etc:

My database table is like this:

cid int(11) No auto_increment
parentid int(11) No 0
img varchar(30) latin1_swedish_ci No
title varchar(40) latin1_swedish_ci No
cdesc text latin1_swedish_ci No
hits

Currently it is diaplying straight as:

categories
and after finish of main categories .

sub ategories start.

i am using the query as;

select cid, title, parentid
from grcat
order by cid

<cfselect name="category" label="Select Category: " value="cid" display="title" query="getCats" tabindex="1" tooltip="Chooose Your category"></cfselect>

i want is my subcategories should show after their maincategory

like:

autos/car
autos/files

in the same single select.

I trying it but no possible solution working me instead
    This topic has been closed for replies.

    3 replies

    Inspiring
    December 4, 2008
    Hi,

    There's a lot of nice info in this article:
    http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

    What I usually do is to save a node-sort and node-indent value into the database. That helps out a lot with dealing with nested-set problems. It's not fully normalized, but the sort and indent values can always get re-created whenever you do any changes to the tree structure.

    cheers,
    fober
    Inspiring
    December 3, 2008
    Hi,

    Try replacing your existing order by clause with parentid,cid asc;
    Inspiring
    December 3, 2008
    Hi,

    Concatenate your maincategory and subcategory (place a "/" between them) as "title" in your "getCats" query

    SELECT maincategory + '/' + subcategory as title,your_other_columns FROM getCats

    and then do the display in your <cfselect> as,

    <cfselect name="category" label="Select Category: " value="cid" display="title" query="getCats" tabindex="1" tooltip="Chooose Your category"></cfselect>
    Inspiring
    December 3, 2008
    Thanks Dude. Bu the The gravity of Situation is here that I have category and Subcategory under one column and that is title.

    That i want to show as i have parentid field within the table as:

    cid, parentid, img, title, desc

    So title values having parentid of 0 are main categories
    and once the next set of categories whose parentid stat from 1,2,3 are sub categories.

    I am bit confused at this