Hi, I have a table thats laid out something like the following.... - cat_id, cat_name, cat_parent
-
1 furniture null
-
2 electrical null
-
3 tvs 2
-
4 desks 1
-
5 corner desks 4
-
6 sony tvs 3
-
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
3 2193
Hi, I have a table thats laid out something like the following.... - cat_id, cat_name, cat_parent
-
1 furniture null
-
2 electrical null
-
3 tvs 2
-
4 desks 1
-
5 corner desks 4
-
6 sony tvs 3
-
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?
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?
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 - getTreeArray(int cat_id) {
-
select cat_id, cat_parent,cat_name from tableName into cat, parent,name
-
array[cat_id] = name
-
while(parent != null) {
-
select cat_id, cat_parent,cat_name from tableName) into cat, parent,name
-
array[cat_id] = name
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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. ...
|
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...
|
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
{
|
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...
|
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. ...
|
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
|
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...
|
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:
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |