EasyCFM.COM Forum / Coding Help! / showing category and Sub category:

   Reply to Discussion | New Discussion << previous || next >> 
Posted By Discussion Topic: showing category and Sub category:

book mark this topic Printer-friendly Version  send this discussion to a friend  new posts last

specific
12-03-2008 @ 1:17 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 889
Joined: Apr 2006

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Door to Coldfusion Community
Will Remain Open Till World Ends

Want a Web Portal Contact Me

randhawaz81@gmail.com

<cf_visitwebsite>

http://portal.randhawaworld.com/

</cf_visitwebsite>

Webmaster
12-06-2008 @ 2:39 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4412
Joined: Jan 2002

Not: I posted a more descriptive blog entry on this; with data example.. in case you wanna check it out)

http://co.ldfusion.com/index.cfm/2008/12/6/What-is-the-best-way-to-show-cateogies-and-their-children-and-their-children-and-those-children

To achieve this easiy; here is how I usually do it.

First I create a custom tag.

<!--- categories.cfm --->
<cfparam name="attributes.parentID" default="0" />

<cfif thisTag.executionMode eq "Start">
<!--- get al the categories that have a parentID defined --->
<cfquery name="qGetCategories" datasource="#request.dsn#">
  select categoryID, category
  from categories
  where parentID = #val(attributes.parentID)#
</cfquery>

<!--- now display them --->
<cfoutput query="qGetCategories">
  <a href="#categoryID#">#category#</a><br />
  <!--- now here call this tag again, this time with this categoryID --->
  <cf_categories parentID="#categoryID#" />
</cfoutput>

</cfif>


Now on your main page (where you want to show them) you simply do an initial call:

<cf_categories parentID="0" />


Since the first call is provided with a "0" it will load the parent ones... then as you begin to loop through; it will call itself in as many times as there are child categories... So you can go as many levels as you want.

If you only want to go two levels; then you can do something like this:


<!--- now display them --->
<cfoutput query="qGetCategories">
  <a href="#categoryID#">#category#</a><br />
  <!--- now here call this tag again, this time with this categoryID --->
  <cfif attributes.parentID eq 0>
  <cf_categories parentID="#categoryID#" />
  </cfif>
</cfoutput>


That will only loads the first level of children... hope this helps... let me know if you have questions!

P

Pablo Varando
Senior Application Architect
EasyCFM.COM, LLC.

904.483.1457 \\ mobile
webmaster@easycfm.com \\email

\m/ (>.<) \m/
--- rock on ---

This message was edited by Webmaster on 12-6-08 @ 3:12 AM




Copyright © 2002 - 2009. EasyCFM.COM, LLC.
Powered by < CF FORUM > v.2.0
 
Download the EasyCFM.COM Browser Toolbar!
EasyCFM Search Provider Add Search Provider Don't Show Note