Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Looping Through Table Display Category Hierarchy

Looping Through Table Display Category Hierarchy

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tech untouchable
    wrote on last edited by
    #1

    I have my category hiearchy stored within a table called categories. What I need to do is display the actual category hiearchy with a single sql statement. For example: I have the following data in the table category | category_no | category_above --------------------------------------------------- Computers | 1 | 0 Dell | 24 | 1 Sony | 48 | 1 Pentium 4 | 33 | 24 Pentium 4 | 85 | 48 How would i query a database to sort the records as follows (in the category hierarchy)? Computers, Dell, Pentium 4, Sony, Pentium 4 so i could then generate text that looks like Computers -->Dell ----->Pentium 4 -->Sony ----->Pentium 4 Anyone have any ideas? Right now I have a function that loops through, but the problem is it's using too many resources...I have to open a database connection each time i loop through. So if I had 500 categories i'd be opening 500 database connections, which i don't want. Any help?

    S 1 Reply Last reply
    0
    • T tech untouchable

      I have my category hiearchy stored within a table called categories. What I need to do is display the actual category hiearchy with a single sql statement. For example: I have the following data in the table category | category_no | category_above --------------------------------------------------- Computers | 1 | 0 Dell | 24 | 1 Sony | 48 | 1 Pentium 4 | 33 | 24 Pentium 4 | 85 | 48 How would i query a database to sort the records as follows (in the category hierarchy)? Computers, Dell, Pentium 4, Sony, Pentium 4 so i could then generate text that looks like Computers -->Dell ----->Pentium 4 -->Sony ----->Pentium 4 Anyone have any ideas? Right now I have a function that loops through, but the problem is it's using too many resources...I have to open a database connection each time i loop through. So if I had 500 categories i'd be opening 500 database connections, which i don't want. Any help?

      S Offline
      S Offline
      sreejith ss nair
      wrote on last edited by
      #2

      Try to normalize yout category table. That will be a better solution for this. Once you normalize, you can show the data in hierarchy. Then use Datarelation to show the data. Sreejith Nair [ My Articles ]

      A 1 Reply Last reply
      0
      • S sreejith ss nair

        Try to normalize yout category table. That will be a better solution for this. Once you normalize, you can show the data in hierarchy. Then use Datarelation to show the data. Sreejith Nair [ My Articles ]

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Can you please explain a little further and give me an example... How would it work say if I had two tables: If the first table would carry the top level categories (Main) and the second table would then keep the sub level categories...how would that benefit me? I want to make sure that I don't limit the users from creating category levels.

        S 1 Reply Last reply
        0
        • A Anonymous

          Can you please explain a little further and give me an example... How would it work say if I had two tables: If the first table would carry the top level categories (Main) and the second table would then keep the sub level categories...how would that benefit me? I want to make sure that I don't limit the users from creating category levels.

          S Offline
          S Offline
          sreejith ss nair
          wrote on last edited by
          #4

          Try to use DataRelation class for further reference. Sreejith Nair [ My Articles ]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups