Navid,

So you want something like this:

Category 1
Category 2
    |
     ---> Category a
    |
    ----> Category b
                 |
                  ----> Category X
                 |
                  ----> Category Y
                               |
                                ----> Category AC
                               |
                                ----> Category BC
                  |
                   ----> Category Z
    |
     ----> Category c
Category 3

in a multi-dimentional array????

Jordan S. Jones



Navid Yar wrote:

Hello Guys,

I need a little bit of help with recursion. I've searched our PHP website
and Google, but none helped me understand my problem. There is a code below
this message in order to help you understand what I am trying to achieve.
Here is an explaination:

What I'm trying to do is list a typical category/subcategory system with
parents and children associated with those parents. My database table
(categories) lists all the parents and children together, each with a
parent_id field
(with root being a value of 0). What I want to do is, if a user clicked on
one of the parent categories, only one level of that category will show (or
only the direct children of that specific category will show). I want the
depth to be endless because I want to control the depth some other way. I
know that using a recursive theory would cost a lot as far as speed goes,
but I'm willing to risk it for now.

Here is the problem: The problem is that the recursive method yields several
arrays, instead of one long array. I tried to use an array_push() function,
but that doesn't seem to work well with multi-dimentional arrays. I tried
the straight way, i.e. $menu_array[$count]['name'] = $name_of_category or
$menu_array['name'][$count] = $name_of_category, but that yields several
arrays instead of one long array or category names. The depth of the
categories can be determined by the $_GET string passed, $_GET['some_path'],
which is in the format: parent1_child1_grandchild1_grandchild2,
etc.($some_path = 1_4_6_8), where all of these are related to each other.
These, of course, are split using the underscore delimeter: $path['0'] = 1,
$path['1'] = 4, and so on.

And finally, here is my question: How do I get all these categories, parents
and children, listed into one array and then returned. I want to be able to
list them on the web page using one array. I will also include id,
parent_id, and other info with each array, but first I want to get the name
listings of the categories to work. Also, if anyone has any suggestions
about a more speedier way to do this, please let me know.

Sorry for the long explaination, I just wanted to make sure you guys
understood my goals. Thanks in advance to anyone that responds, I appreciate
it very much. Here is the code I promised:

-----------------------------------------

function menu_tree($parent_id = '0', $cPath = '', $menu_array = '') {
   if (!is_array($menu_array)) {
       $menu_array = array();
       $cPath = $this->separatePath($_GET['cPath']); // separates $_GET
string into array of category ids
   } else {
       reset($cPath);
       array_shift($cPath);
   }

   if (sizeof($cPath) >= 0) {
       $db = new base_db();
       $query = "select cid, name, parent_id from categories where
parent_id = '" . $parent_id . "' order by sort_order, name;";
       $categories = $db->fetch_results($query);
       //echo sizeof($cPath)."<br />";
       //echo $query."<br />";

       for ($i = 0, $count = 0; $i < count($categories); $i++, $count++) {
          // The following are the methods I tied, but failed to work
           //$menu_array['name'][$count] = $categories[$i]['name'];
           //$menu_array[]['name'] = $categories[$i]['name'];
           //$menu_array['name'][] = $categories[$i]['name'];
                //array_push($menu_array, $categories[$i]['name']); // This one works, 
but
does not yeild a multi-dimensional array, which is what I need if I were to
add more information to the output of this array, like id and parent_id
           //array_push($menu_array[$count]['name'],
$categories[$i]['name']); // This does not work, gives error saying the
first parameter of array_push must be an array

           if (($this->get_children($categories[$i]['cid'])) &&
in_array($categories[$i]['cid'],$cPath)) {
               $this->menu_tree($categories[$i]['cid'], $cPath,
$menu_array);
           }
       }
   }
   print_r($menu_array);
}

-----------------------------------------
Here is what it returns using the print_r() function on $menu_array

Array ( [name] => Array
       (
           [0] => Cars
           [1] => Honda
           [2] => Accord
           [3] => 1996
           [4] => 1997
           [5] => 1998
           [6] => 1999
           [7] => 2000
           [8] => 2001
           [9] => 2002
       )
)
Array ( [name] => Array
       (
           [0] => Cars
           [1] => Honda
           [2] => Accord
           [3] => Civic
       )
)
Array
(
   [name] => Array
       (
           [0] => Cars
           [1] => Honda
           [2] => Toyota
       )
)
Array
(
   [name] => Array
       (
           [0] => Cars
           [1] => Suvs
           [2] => Trucks
       )
)




-- I am nothing but a poor boy. Please Donate.. https://www.paypal.com/xclick/business=list%40racistnames.com&item_name=Jordan+S.+Jones

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to