473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

recursive sql

107 100+
Hi, I have a table thats laid out something like the following....


Expand|Select|Wrap|Line Numbers
  1. cat_id, cat_name, cat_parent
  2. 1        furniture          null
  3. 2        electrical          null
  4. 3        tvs           2
  5. 4        desks                  1
  6. 5        corner desks   4       
  7. 6        sony tvs        3
  8. 7        wardrobe        1 

my question is, what's the best way of finding the full category list given 1 category id. I really want to return the result as an array of category names, with the category id's being the array keys.

currently im trying to make a recursive function, but i thought maybe there is a better (more efficient?) way using a do while() loop. Or even a way of recursing with an sql statement?


Thanks for any help!


Andy
Apr 29 '08 #1
3 2193
r035198x
13,262 8TB
Hi, I have a table thats laid out something like the following....


Expand|Select|Wrap|Line Numbers
  1. cat_id, cat_name, cat_parent
  2. 1        furniture          null
  3. 2        electrical          null
  4. 3        tvs           2
  5. 4        desks                  1
  6. 5        corner desks   4       
  7. 6        sony tvs        3
  8. 7        wardrobe        1 

my question is, what's the best way of finding the full category list given 1 category id. I really want to return the result as an array of category names, with the category id's being the array keys.

currently im trying to make a recursive function, but i thought maybe there is a better (more efficient?) way using a do while() loop. Or even a way of recursing with an sql statement?


Thanks for any help!


Andy
Could you post an example? e.g What should the result be when the id given is, say, 3?
Apr 29 '08 #2
theS70RM
107 100+
Could you post an example? e.g What should the result be when the id given is, say, 3?
sure,

if given the value 6, it should return an array of...

myarr['6'] => "sony tvs"
myarr['3'] => "tv's"
myarr[2] => "electrical"


you follow?
Apr 29 '08 #3
r035198x
13,262 8TB
sure,

if given the value 6, it should return an array of...

myarr['6'] => "sony tvs"
myarr['3'] => "tv's"
myarr[2] => "electrical"


you follow?
So you want to do this using a PHP function?
I'm not good with PHP but your agorighm will be something like

Expand|Select|Wrap|Line Numbers
  1. getTreeArray(int cat_id) {
  2.         select  cat_id, cat_parent,cat_name  from tableName into cat, parent,name
  3.          array[cat_id] = name
  4.         while(parent != null) {
  5.                 select  cat_id, cat_parent,cat_name  from tableName) into cat, parent,name
  6.                array[cat_id] = name
  7.       }
  8. }
Apr 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
9
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
0
by: champ1979 | last post by:
I wrote an algorithm to get all the relatives of a person in a family tree. I'm basically getting all the users from the DB and am doing the recursive logic in code, so that there is only 1 call...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.