views:

438

answers:

5

I have a need to show a select box which will display all categories and subcategories in one go.

I want to show All Categories left most & bold while all sub categories will come under respective Categories but will be indented and Italized.

How can we create such a Select List in PHP?

I have something like this in Magento Ecommerce (www.magentocommerce.com) admin panel.

The categories are retrieved from DB and at times can go upto 6 levels deep for example:

`Cat 1

Cat 1A

Cat 1B

Cat 2

Cat 2A

Cat 2AA

Cat 2AB

Cat 2AC

Cat 2ACA

Cat 3`

etc. All categories that have sub categories should be Bold and all sub categories should be Italics.

I hope now it is more clear as to what I want to achieve.

A: 

Well, you could just add a couple of  's before each element you want to indent, like so:

<select id="mySelect">
    <option value="topLevelFoo">Foo</option>
    <option value="subBar">& nbsp;& nbsp;Bar</option>
    <option value="subBaz">& nbsp;& nbsp;Baz</option>
</select>

It's a little ugly, but it should be enough to accomplish your goal (at least visually). Another common way to do this is to use hyphens to indicate these types of choices.

Good luck!

Edit: Er, it looks like the SO editor is rendering the & nbsp; elements. I've added spaces after the ampersand to get around this, but the spaces wouldn't actually appear in your code.

inkedmn
+4  A: 

You have to use the optgroup element in your select element:

<select name="Namen" size="6">
  <optgroup label="Namen mit A">
     <option label="Anna">Anna</option>
     <option label="Achim">Achim</option>
     <option label="August">August</option>
  </optgroup>
  <optgroup label="Namen mit B">
     <option label="Berta">Berta</option>
     <option label="Barbara">Barbara</option>
     <option label="Bernhard">Bernhard</option>
  </optgroup>
</select>

You can then style them via

optgroup {
    font-weight:bold;
    /*etc*/
}
smoove666
Thanks for this but the sub items levels are retrieved from DB and there can be more than one sub. For Example:Cat 1 Cat 1A Cat 1AACat 2 Cat 2A Cat 2B Cat 2BA Cat 2BBCat 3Cat 4Cat 5 Cat 5Aetc.So how can I achieve this?
Yogi Yang 007
Hmm, if it gets a more complicated list, you are probably better off not using optgroup (you can't select optgroup elements from the list).You could try to give the option elements classes like level1, level2 etc. and adjust the padding / font weight via those classes.
smoove666
A: 

use the optgroup tag to bold the categories.

<html>
    <body>
            <select>
                    <optgroup label="Numbers">
                            <option value="1">One</option>
                            <option value="2">Two</option>
                            <option value="3">Three</option>
                    </optgroup>
                    <optgroup label="Letters" disabled="true">
                            <option value="4">A</option>
                            <option value="5">B</option>
                            <option value="6">C</option>
                    </optgroup>
            </select>
    </body>

TheBrain
A: 

Look in the Html source where you have something like what you want, then you know what code php will have to output. And then you have to do that. How exactly that is done depends on how your data is stored etc. If your Question is not solved, get more specific

Flo
A: 

This has nothing to do with PHP, what you need is some HTML and CSS to style it.

You can make a list with identations like this:

<select class="category">
    <option>Category</option>
    <optgroup label="Optional label">
        <option>More options</option>
    </optgroup>
</select>

You can then style it like this:

.category option {
    font-weight: bold;
}

.category optgroup option {
    font-style: italic;
}
Mythica