Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through unlimited nested categories

Status
Not open for further replies.

oohoohoohooh

Programmer
Apr 30, 2005
55
GB
Hi, I have a table setup called categories with the following structure:

- cat_id (primary key)
- subcat_id
- cat_name

If a category is within another category it's subcat_id would be the cat_id of the category it is within. This allows me to have unlimited subcats within subcats. Hope all is clear so far. I setup the following function to store the contents of the table in an array (so I don't have lots of queries):

Code:
function get_categories() {
	$cats	= array();

	$query = $GLOBALS['db']->query("SELECT * FROM " . $GLOBALS['config']['db']['prefix'] . "cats ORDER BY cat_id");

	while ($cat = $GLOBALS['db']->fetch_array($query)) {
		$cats[$cat['subcat_id']][] = $cat;
	}

	return $cats;
}

Now I need some way of looping through the array to display the contents. This is where I get stuck as I'm not too great with arrays. Below is the kind of output I wish to display based on the array at the bottom:

Code:
Books, Music, DVD
   - Books
   - DVD
   - Music
      - Pop
      - Rock
   - Video
Electronics & Office
   - Electronics
   - Mobile Phones
   - Camera & Photo

I put print_r(get_categories()) so you could see the contents of the array we're dealing with and I got:

Code:
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [cat_id] => 1
                    [subcat_id] => 0
                    [cat_name] => Books, Music, DVD
                )

            [1] => Array
                (
                    [cat_id] => 2
                    [subcat_id] => 0
                    [cat_name] => Electronics & Office
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [cat_id] => 5
                    [subcat_id] => 1
                    [cat_name] => Books
                )

            [1] => Array
                (
                    [cat_id] => 6
                    [subcat_id] => 1
                    [cat_name] => DVD
                )

            [2] => Array
                (
                    [cat_id] => 7
                    [subcat_id] => 1
                    [cat_name] => Music
                )

            [3] => Array
                (
                    [cat_id] => 8
                    [subcat_id] => 1
                    [cat_name] => Video
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [cat_id] => 9
                    [subcat_id] => 2
                    [cat_name] => Electronics
                )

            [1] => Array
                (
                    [cat_id] => 10
                    [subcat_id] => 2
                    [cat_name] => Mobile Phones
                )

            [2] => Array
                (
                    [cat_id] => 11
                    [subcat_id] => 2
                    [cat_name] => Camera & Photo
                )

        )

    [7] => Array
        (
            [0] => Array
                (
                    [cat_id] => 26
                    [subcat_id] => 7
                    [cat_name] => Pop
                )

            [1] => Array
                (
                    [cat_id] => 27
                    [subcat_id] => 7
                    [cat_name] => Rock
                )

        )

)

I'd appreciate it if you could help. Thanks
 
To paraphrase the movie "The Graduate":

I want to say one word to you. Just one word. Recursion.


If there is no explicit limit to the depth of nesting, then you are going to have to write a recursive function to list all members.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top