0

I have category table:

Category table:
cat_id      cat_pid     cat_name
    1           0       Women's Fashion
    2           1       Women Accessories
    3           2       Wallets & Cardholders
    4           3       Cases

I have parent level category id which is "Women's Fashion (cat_id = 1)" and leaf level category id which is "Cases (cat_id = 4)"

I want to find all categories between 4 and 1.

Array like this:

Array(
     [0]=>2
     [1]=>3
)
okconfused
  • 3,567
  • 4
  • 25
  • 38
  • check these links http://stackoverflow.com/questions/18163449/php-nested-menu-with-a-recursive-function-expand-only-some-nodes-not-all-the --------------------------------- http://stackoverflow.com/questions/10782810/echo-menu-tree-with-recursive-function – Qazi Dec 01 '15 at 07:13

1 Answers1

1

i don't know if you are using mysqli or PDO so i wrote the code on basics...

<?php

$cats = [];

function find_parent($parent_id){
    global $cats;
    if ($parent_id > 0){
        $q = mysql_query('SELECT cat_pid FROM Category WHERE cat_id = ' . $parent_id);
        $r = mysql_result($q, 0, 'cat_pid');
        if ($r > 0){
            $cats[] = $parent_id;
            find_parent($r);
        }

    }
}

find_parent(4);
array_shift($cats); //it adds first record too and i think you don't want so removing it here...
print_r($cats);
Murat Cem YALIN
  • 317
  • 1
  • 6