0

hi i am using codeigniter . i have a table like this .

PreferenceID | PreferencParentID |   Value
   1         |      0            | Fashion
   2         |      0            | music
   3         |      1            | Men's fashion
   4         |      1            | Women's fashion
   5         |      2            | Rock music 
   6         |      3            | shirts

if i passed PreferenceID = 1 i want to get records Fashion,Men's fashion,Women's fashion,shirts

and if i passed PreferenceID = 2 i want to get records music ,Rock music .

  • i want to get all the child values in all levels when i pass a parent id . here if i passed PreferenceID = 1 it is Fashoin , it has two childs , Men's fashion and Women's fashion . and men's fashion is the parent value of shirts .

this is the hierarchy . it is like a tree structure , and goes to N levels , please help me

enter image description here

currently i am doing is passing prefernceID and get all next level childs ,

ex :- i am passing prefernceID = 1 and getting only Men's fashion and Women's fashion :(

please help . thanks in advance

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • 1
    Refer this http://stackoverflow.com/questions/169817/is-it-possible-to-query-a-tree-structure-table-in-mysql-in-a-single-query-to-an – Madhivanan Jul 20 '12 at 06:56
  • Is there any max depth which you plan to have? Like 3 or 4, or do you plan on this to be as deep as possible? – Robin Castlin Jul 20 '12 at 11:25

2 Answers2

0

There's no way to do that via the AR class. Try this:

$sql = 'SELECT `PreferenceID`, `PreferencParentID`, `Value`
        FROM `YourTableName`
        WHERE 1 IN(`PreferenceID`, `PreferencParentID`)';
$this->db->query($sql);

(assuming that you use MySQL)

Narf
  • 14,600
  • 3
  • 37
  • 66
  • actually OP is asking for fetching sub-sub children of 1 as well i.e to include "shirts" from above data for ID 1. – TigerTiger Jul 20 '12 at 11:07
0

Maybe you should use MySQL procedure or adapter view.

Example data like you post:

mysql> create table treeNodes
    -> (
    ->  id int primary key,
    ->  nodename varchar(20),
    ->  pid int
    -> );
Query OK, 0 rows affected (0.09 sec) 
mysql> select * from treenodes;
+----+----------+------+
| id | nodename | pid  |
+----+----------+------+
|  1 | A        |    0 |
|  2 | B        |    1 |
|  3 | C        |    1 |
|  4 | D        |    2 |
|  5 | E        |    2 |
|  6 | F        |    3 |
|  7 | G        |    6 |
|  8 | H        |    0 |
|  9 | I        |    8 |
| 10 | J        |    8 |
| 11 | K        |    8 |
| 12 | L        |    9 |
| 13 | M        |    9 |
| 14 | N        |   12 |
| 15 | O        |   12 |
| 16 | P        |   15 |
| 17 | Q        |   15 |
+----+----------+------+
17 rows in set (0.00 sec)

the tree level is:

 1:A
  +-- 2:B
  |    +-- 4:D
  |    +-- 5:E
  +-- 3:C
       +-- 6:F
            +-- 7:G
 8:H
  +-- 9:I
  |    +-- 12:L
  |    |    +--14:N
  |    |    +--15:O
  |    |        +--16:P
  |    |        +--17:Q
  |    +-- 13:M
  +-- 10:J
  +-- 11:K  

you could create a function getChildLst to get a string that contains all your child node.

mysql> delimiter //
mysql>
mysql> CREATE FUNCTION `getChildLst`(rootId INT)
    -> RETURNS varchar(1000)
    -> BEGIN
    ->   DECLARE sTemp VARCHAR(1000);
    ->   DECLARE sTempChd VARCHAR(1000);
    ->
    ->   SET sTemp = '$';
    ->   SET sTempChd =cast(rootId as CHAR);
    ->
    ->   WHILE sTempChd is not null DO
    ->     SET sTemp = concat(sTemp,',',sTempChd);
    ->     SELECT group_concat(id) INTO sTempChd FROM treeNodes where FIND_IN_SET(pid,sTempChd)>0;
    ->   END WHILE;
    ->   RETURN sTemp;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;

then we can use find_in_set function find all the value:

mysql> select getChildLst(1);
+-----------------+
| getChildLst(1)  |
+-----------------+
| $,1,2,3,4,5,6,7 |
+-----------------+
1 row in set (0.00 sec) 

mysql> select * from treeNodes
    -> where FIND_IN_SET(id, getChildLst(1));
+----+----------+------+
| id | nodename | pid  |
+----+----------+------+
|  1 | A        |    0 |
|  2 | B        |    1 |
|  3 | C        |    1 |
|  4 | D        |    2 |
|  5 | E        |    2 |
|  6 | F        |    3 |
|  7 | G        |    6 |
+----+----------+------+
7 rows in set (0.01 sec)

mysql> select * from treeNodes
    -> where FIND_IN_SET(id, getChildLst(3));
+----+----------+------+
| id | nodename | pid  |
+----+----------+------+
|  3 | C        |    1 |
|  6 | F        |    3 |
|  7 | G        |    6 |
+----+----------+------+
3 rows in set (0.01 sec)
fuyunbiyi
  • 38
  • 2