SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    I Never Give Up roosevelt's Avatar
    Join Date
    May 2005
    1 Post(s)
    0 Thread(s)

    Post Generating Drop Down Menu from DB Rows... recursively!

    Sorry the title is bit sloppy but for sometime now I have been trying to do this but poor luck my recursive method creation skills really suck .

    Anyways I have a table like below:

    - CatId
    - CatName
    - CatDesc
    - CatParent

    and some values like below

    1 - Photoshop - Some photoshop stuff - 0
    2 - Web Design - Web layouts - 1
    3 - XHTML - XHTML layout specific tutorials - 2

    Basically XHTML is a child category of Web Design and Web Design is a child of Photoshop.

    First I tried to do this

    1) Grab the parent category (CatParent is set to 0) using a loop
    2) Inside that loop I got another loop to grab its childs.
    3) Another loop to get the child of childs .

    while(get photoshop)
    get web design
    while(get web design)
    get XHTML

    So you see I will have to keep adding loops in order to dig deeper each level. So can anyone please tell me how I can do this recursively so that I don't have to keep adding loops? and generate something like below

    - Web Design
    -- XHTML
    --- Another
    ----- Yet Another

    - Test 1
    -- Test 2
    --- Test 3

    Thanks in advance!

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Victoria, Australia
    29 Post(s)
    2 Thread(s)

    I usually create a multidimensional array from a db stucture like this.

    Then itterating through it is simple using a set of foreach loops
    Code php:
    foreach($array as $key => $value) {
      //Do something with the array
      foreach($array as $key => $value) {
        // Do something with the next array
        foreach($array as $key => $value) {
          // and so on and so forth


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts