Looping Through Table Display Category Hierarchy
-
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?
-
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?
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 ]
-
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 ]
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.
-
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.
Try to use DataRelation class for further reference. Sreejith Nair [ My Articles ]