Greytree

TamWiki

For a mouse who is a packrat

Technology » Nested Options In Select
How to handle options if you've got a nested set of them: optgroup tag

Summary:this is what goes at the top of the site

(redirected from Main.NestedOptionsInSelect)

Up: ^HTML?^

From a recent php-general mailing list entry, someone was wondering how to set up a select block if you have nested items to select from. The way to do this is with the <optgroup> tag, which, as the name suggests, groups sets of options:

  1. <p>A selection widget about cars. Notice groupings.</p>
  2. <select>
  3.     <optgroup label="Swedish Cars">
  4.         <option value="volvo">Volvo</option>
  5.         <option value="saab">Saab</option>
  6.     </optgroup>
  7.     <optgroup label="German Cars">
  8.         <option value="mercedes">Mercedes</option>
  9.         <option value="audi">Audi</option>
  10.     </optgroup>
  11. </select>
Unselected
Attach:ex1unselected.png Δ
Drop Down Enabled
Attach:ex1dropdown.png Δ

As you can see above, there are two groups, "Swedish Cars" and "German Cars" with different options inside of them.

You can put optgroups inside of optgroups for further nesting:

  1. <p>Another selection widget about cars. Notice the nested groupings</p>
  2. <select>
  3.     <optgroup label="Swedish Cars">
  4.         <optgroup label="&nbsp;Volvo">
  5.             <option value="240s">240 Sedan</option>
  6.             <option value="240w">240 Wagon</option>
  7.             <option value="740s">740 Sedan</option>
  8.             <option value="740w">740 Wagon</option>
  9.         </optgroup>
  10.         <optgroup label="&nbsp;Saab">
  11.             <option value="900s">900s</option>
  12.         </optgroup>
  13.     </optgroup>
  14.     <optgroup label="German Cars">
  15.         <optgroup label="&nbsp;Mercedes">
  16.             <option value="300SD">300SD</option>
  17.             <option value="S350">S350</option>
  18.             <option value="400SE">400SE</option>
  19.             <option value="500SEL">500SEL</option>
  20.             <option value="600SEL">600SEL</option>
  21.         </optgroup>
  22.         <optgroup label="&nbsp;Audi">
  23.             <option value="2011TTSCoupe">2011 TTS Coupe</option>
  24.             <option value="2011TTSRoadster">2011 TTS Roadster</option>
  25.             <option value="S5Coupe">S5 Coupe</option>
  26.             <option value="S5Cabriolet">S5 Cabriolet</option>
  27.             <option value="S6Sedan">S6 Sedan</option>
  28.         </optgroup>
  29.     </optgroup>
  30. </select>

Notice the use of the &nbsp; in the example above to set off the supgroupings a little from the parent.

Unselected
Attach:ex2unselected.png Δ
Drop Down Enabled
Attach:ex2dropdown.png Δ

Creating nested options via PHP

This should be drop dead easy, but I'll include an example anyway.

  1. <?php
  2. $categories[] = array('id' => 1, 'name' => 'cars');
  3. $categories[] = array('id' => 2, 'name' => 'trucks');
  4. $categories[] = array('id' => 3, 'name' => 'motorcycles');
  5.  
  6. $pages[] = array('id' => 1, 'name' => 'Neon', 'cat_id' => 1);
  7. $pages[] = array('id' => 2, 'name' => 'Saturn', 'cat_id' => 1);
  8. $pages[] = array('id' => 3, 'name' => 'F150', 'cat_id' => 2);
  9. $pages[] = array('id' => 4, 'name' => 'Ram 2500', 'cat_id' => 2);
  10. $pages[] = array('id' => 5, 'name' => 'Suzuki', 'cat_id' => 2);
  11. $pages[] = array('id' => 6, 'name' => 'Honda', 'cat_id' => 3);
  12.  
  13. echo "<select id=\"page_id\">\n";
  14.  
  15. foreach ($categories AS $cat) {
  16.     $c_cat_name = htmlspecialchars($cat['name']);
  17.     echo "\t<optgroup label=\"{$c_cat_name}\">\n";
  18.  
  19.     foreach ($pages AS $page) {
  20.         if ( $page['cat_id'] == $cat['id'] ) {
  21.             $c_page_id = htmlspecialchars((int)$page['id']);
  22.             $c_page_name = htmlspecialchars($page['name']);
  23.             echo "\t\t<option value=\"{$c_page_id}\">{$c_page_name}</option>\n";
  24.         }
  25.     }
  26.     # Reset pages so you can loop through it again
  27.     reset($pages);
  28.     echo "\t</optgroup>\n";
  29. }
  30.  
  31. echo "</select>\n";
  32. ?>

Which returns the following HTML:

  1. <select id="page_id">
  2.     <optgroup label="cars">
  3.         <option value="1">Neon</option>
  4.         <option value="2">Saturn</option>
  5.     </optgroup>
  6.     <optgroup label="trucks">
  7.         <option value="3">F150</option>
  8.         <option value="4">Ram 2500</option>
  9.         <option value="5">Suzuki</option>
  10.     </optgroup>
  11.     <optgroup label="motorcycles">
  12.         <option value="6">Honda</option>
  13.     </optgroup>
  14. </select>


Tags: Categories: Articles

Recent Changes | Printable View | Page History | Edit Page
Page last modified on April 17, 2012, at 08:59 PM by ImportText?