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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:17:14+00:00 2026-05-16T07:17:14+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 because I re-worked the code a little.

I was wondering 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 to display the select drop down.

echo '<select name="parent_id">
      <option value="0">None</option>';

function make_list ($parent) {

    global $option;

    foreach ($parent as $id => $cat) {

        echo '<option value="' . $cat['id'] . '">' . $cat['category'] . '</option>';

        if (isset($option[$id])) { 

            make_list($option[$id]);

        }                       
    }       
}

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");

if (!$dbc) {
    print mysqli_error();
} 

$option = array();

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

    $option[$parent_id][$id] =  array('category' => $category, 'id' => $id, 'parent_id' => $parent_id);

}

make_list($option[0]);

echo '</select>';

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-16T07:17:15+00:00Added an answer on May 16, 2026 at 7:17 am

    I see you already have the recursion thing down — try passing a “depth” parameter in your make_list function — when you first call it, you’d set $depth to zero. Then, when you call it recursively, you’d say makelist($option[$id], $depth+1);

    knowing what level of recursion you are on, you could easily construct a string of whitespace. Like:

    $whitespace = str_repeat('&nbsp;', $depth);
    

    for more indentation, try str_repeat('&nbsp;', $depth * 2); or similar

    then just prepend your option with the whitespace.

    • 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
Earlier, I asked a similar question, but I've since changed my code. Now the
I have already asked a similar question earlier but I have notcied that I
I asked a similar question earlier, but posed it as a language-specific question. I
I'd asked a very similar question earlier , but the project scope is quickly
I asked an earlier question similar to this (with no responses), but I know
This is a similar question I asked earlier, except different in the fact that
i have asked a similar question earlier, but this time i need a method
I asked a similar question recently, but didn't get a clear answer because I
A similar question was asked earlier there , but the question here is the

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.