Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:31:10+00:00 2026-05-30T15:31:10+00:00

I have a categories table that looks like this: —————————————- | id | parentId

  • 0

I have a categories table that looks like this:

----------------------------------------
|  id   |  parentId  |  Name           |
----------------------------------------
   1          0         Cat 1
   2          0         Cat 2
   3          0         Cat 3
   4          2         Cat 4
   5          3         Cat 5
   6          5         Cat 6

Basically I need to iterate through the categorys creating a UL LI html list like the following:

<ul id="categories">
    <li id="1">Cat 1</li>
    <li id="2">Cat 2
       <ul>
           <li id="4">Cat 4</li>
       </ul>
    </li>
    <li id="3">Cat 3
        <ul>
           <li id="5">Cat 5
               <ul>
                   <li id="6">Cat 6</li>
               </ul>
           </li>
        </ul>
    </li>
</ul>

Im having major issues trying to iterate over this trying to create the above html. The id’s maybe any number of levels deep within parentId’s. Im donig this in PHP. Because there are nth number of levels deep I think I need to do some kind of array_walk funuction but not surch how. Also to make things just a little harder the machine it sists on runs PHP4 and I know it needs upgrading but it cant at the min so I need a php 4 solution ideally. How should I go about doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:31:11+00:00Added an answer on May 30, 2026 at 3:31 pm

    Try the left/right tree method for storing hierarchical information in a database.

    http://blogs.sitepoint.com/hierarchical-data-database/

    This is what I do in my website, where I have multi-level LIs that need to open at 1:6 and have children 2:3,4:5 where the first number is the ‘left’ and the second is the ‘right’. I have about 5 levels deep at the moment, but you could have many more. It’s just a matter of developing an interface for setting the correct left/right values based on the position you add it to.

    You just have to add a ‘lft’ and ‘rgt’ column to your table (as explained in that article).

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a categories table that looks like this: id | name | parent
I have a table that looks something like this: ID | Keyword | Category
I have a Categories table in which each category has a ParentId that can
Greetings, fellow coders! I have this table that contains categories and subcategories (actually I
I have a table named categories, which contains ID(long), Name(varchar(50)), parentID(long), and shownByDefault(boolean) columns.
I have 3 tables that look like this: tblVideo: VideoID | Video Name 1
I have a table that looks like: What I want to do is output
I have a products table that contains a FK for a category, the Categories
I have a self referencing table named categories that has a parentcategoryid column that
I have a model called Category which looks like this: class Category < ActiveRecord::Base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.