Development

#561 ([PATCH] Add support to create <optgroup> tags from nested arrays in options_for_select())

You must first sign up to be able to contribute.

Ticket #561 (closed enhancement: fixed)

Opened 3 years ago

Last modified 6 months ago

[PATCH] Add support to create <optgroup> tags from nested arrays in options_for_select()

Reported by: Ian Assigned to:
Priority: minor Milestone: 0.6.3
Component: askeet Version: 0.7.X
Keywords: optgroup Cc:
Qualification: Unreviewed

Description

I updated the options_for_select function so that it will detect if an array value ($value) is an array. If so, it creates an <optgroup> with label = $key.

The code is below.

function options_for_select($options = array(), $selected = '', $html_options = array())
{
  $html_options = _parse_attributes($html_options);

  if (is_array($selected))
  {
    $valid = array_values($selected);
    $valid = array_map('strval', $valid);
  }

  $html = '';

  if (isset($html_options['include_custom']))
  {
    $html .= content_tag('option', $html_options['include_custom'], array('value' => ''))."\n";
  }
  else if (isset($html_options['include_blank']))
  {
    $html .= content_tag('option', '', array('value' => ''))."\n";
  }

  foreach ($options as $key => $value)
  {
  	if (is_array($value))
  	{
  	  $html .= content_tag('optgroup', options_for_select($value, $selected, $html_options), array('label' => $key))."\n";
  	}
  	else 
  	{
      $option_options = array('value' => $key);
    	
      if (
          isset($selected)
          &&
          (is_array($selected) && in_array(strval($key), $valid, true))
          ||
          (strval($key) == strval($selected))
         )
      {
        $option_options['selected'] = 'selected';
      }

      $html .= content_tag('option', $value, $option_options)."\n";    	
  	}
  }

  return $html;
}

Change History

05/17/06 18:11:12 changed by Ian

sorry the code formatting got all screwed up =/

05/17/06 18:18:33 changed by Ian

Updated the function to take into account include_custom and include_blank. I had to unset these vars before creating the optgroup or else it would add them underneath each optgroup tag.

function options_for_select($options = array(), $selected = '', $html_options = array())
{
  $html_options = _parse_attributes($html_options);

  if (is_array($selected))
  {
    $valid = array_values($selected);
    $valid = array_map('strval', $valid);
  }

  $html = '';

  if (isset($html_options['include_custom']))
  {
    $html .= content_tag('option', $html_options['include_custom'], array('value' => ''))."\n";
  }
  else if (isset($html_options['include_blank']))
  {
    $html .= content_tag('option', '', array('value' => ''))."\n";
  }

  foreach ($options as $key => $value)
  {
  	if (is_array($value))
  	{
  	  $optgroup_html_options = $html_options;
  	  unset($optgroup_html_options['include_custom']);
  	  unset($optgroup_html_options['include_blank']);
  	  $html .= content_tag('optgroup', options_for_select($value, $selected, $optgroup_html_options), array('label' => $key))."\n";
  	}
  	else 
  	{
      $option_options = array('value' => $key);
    	
      if (
          isset($selected)
          &&
          (is_array($selected) && in_array(strval($key), $valid, true))
          ||
          (strval($key) == strval($selected))
         )
      {
        $option_options['selected'] = 'selected';
      }

      $html .= content_tag('option', $value, $option_options)."\n";    	
  	}
  }

  return $html;
}

05/17/06 18:44:56 changed by Ian

Sample Code to test:

<?php echo select_tag('whatever', options_for_select(array(1 => 'bob', 2 => 'sue', 'group A' => array(4 => 'bill', 5 => 'jerry'), 'Group B' => array(7 => 'john', 8 => 'smith')))); ?>

06/13/06 05:10:53 changed by slickrick

Fixed in r1420

06/13/06 14:01:30 changed by fabien

  • status changed from new to closed.
  • resolution set to fixed.

in r1432