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 994285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:33:06+00:00 2026-05-16T06:33:06+00:00

I asked a similar question earlier but I’ll ask it again in a different

  • 0

I asked a similar question earlier but I’ll ask it again in a different way. How can I indent categories and endless sub categories that I have in a select drop down menu using PHP & CSS?

Here is my PHP code.

while (list($id, $parent_id, $category) = mysqli_fetch_array($r, MYSQLI_NUM)) {

    // Add to the select menu:
    echo '<option value="' . $id . '">' . $category . '</option>\n';

}  

Here is the output.

   1. Apple
   2. Arts & Entertainment
         1. Amusement
         2. Art
         3. Artists
               1. A
                     1. a1
                     2. a2
               2. B
               3. C
               4. D
   3. Automotive
   4. Network
   5. Server
   6. Web Design
         1. CSS
         2. HTML

The numbers are just there to see the categories and sub categories easier.

  • 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-16T06:33:06+00:00Added an answer on May 16, 2026 at 6:33 am

    In the past I’ve done this by prepending &nbsp; to the content of each nested option.

    $spacer = '&nbsp;&nbsp;';
    
    while (list($id, $parent_id, $category) = mysqli_fetch_array($r, MYSQLI_NUM)) {
    
        $padding = str_repeat($spacer, $depth);
    
        // Add to the select menu:
        echo '<option value="' . $id . '">' . $padding . $category . '</option>\n';
    
    }  
    

    Where $depth represents the ‘steps’ an option should be indented.

    The way I accomplished this was to first put my options into a multidimensional array where children are stored as an array of their parent’s…

    Like this:

    Array( 
         1 => Array( 
              'name' => 'Apple'
              ),
         2 => 'Arts & Entertainment',
              'children' => Array(
                   1 => Array(
                        'name' => 'Amusement'
                        ),
                   2 => Array(
                        'name' => 'Art'
                        ),
                   3 => Array(
                        'name' => 'Artists'
                        'children' => Array(
                             1 => Array(
                                  'name' => 'A',
                                  'children' => Array(
                                       1 => Array(
                                             'name' => 'a1'
                                        )
                                  )
                             )
                        )
                   )
              )
         )
     )
    

    Then I run through the array with a recursive function that steps into each nested array (if present) and appends the $padding to the 'name' value, wraps the option tags around it then returns it. The returned string is appended to the return from all previous calls, and you’re left with an indented option menu. Magic.

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

Sidebar

Related Questions

I asked a similar question earlier but I'll ask it again in a different
I have already asked a similar question earlier but I have notcied that I
I asked a similar question earlier How can I have jquery attach behavior to
A similar question was asked earlier there , but the question here is the
I asked a similar question earlier in the week but I don't think my
I asked a similar question about this previously, but I did not specify that
I have asked a similar question before, but I didn't have a firm grasp
A similar question has been asked: MSDN subscriptions on the cheap? , but I
I asked a similar question earlier here , and it got me down the
Sorry folks but I asked a question earlier and perhaps did not explain myself

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.