Part of the EllisLab Network
   
 
Drop down list grouped
Posted: 26 March 2007 07:43 PM   [ Ignore ]  
Summer Student
Total Posts:  1
Joined  03-26-2007

I edited form_dropdown() function (system/helpers/form_helper.php) that can be useful.

<?php

/**
* Drop-down Menu
*
* @access    public
* @param    string
* @param    array
* @param    string
* @param    string
* @return    string
*/    
function form_dropdown($name = '', $options = array(), $selected = '', $extra = '')
{
    
if ($extra != '') $extra = ' '.$extra;
        
    
$form = '<select name="'.$name.'"'.$extra.">\n";
    foreach(
$options as $key => $val)
    
{
        
if(is_array($val))
        
{
            $form
.= '<optgroup label="'.$key.'">'."\n";
            foreach (
$val as $key2 => $val2)
            
{
                $sel
= ($selected != $key2) ? '' : ' selected="selected"';
                
                
$form .= '<option value="'.$key2.'"'.$sel.'>'.$val2."</option>\n";
            
}
            $form
.= "</optgroup>\n";
        
}
        
else
        
{
            $sel
= ($selected != $key) ? '' : ' selected="selected"';
            
$form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
        
}
    }
    $form
.= '</select>';
    
    return
$form;
}

?>
<?php

$so
= array(
        
'Windows' => array(
                
'W95' => '95',
                
'W98' => '98',
                
'WNT' => 'NT',
                
'W2000' => '2000',
                
'WXP' => 'XP'
        
),
        
'Linux' => array(
                
'LRH' => 'RedHat',
                
'LDeb' => 'Debian',
                
'LSuSE' => 'SuSe',
                
'LMDK' => 'Mandrake'
        
)
    );

echo
form_dropdown('so', $so);

?>

<select name="so">
   <
optgroup label="Windows">
      <
option value="W95">95</option>
      <
option value="W98">98</option>
      <
option value="WNT">NT</option>
      <
option value="W2000">2000</option>
      <
option value="WXP">XP</option>
   </
optgroup>
   <
optgroup label="Linux">
      <
option value="LRH">RedHat</option>
      <
option value="LDeb">Debian</option>
      <
option value="LSuSe">SuSe</option>
      <
option value="LMDK">Mandrake</option>
   </
optgroup>
</
select>
Profile
 
 
Posted: 24 April 2008 04:19 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  8
Joined  04-14-2007

How about if I want to pass the optgroup with multiple ? your parameter for the selected not using array. so just one option will have the selected. How about if I want to select more that one option as the default value.. ??

thanks.. smile

Profile
 
 
Posted: 24 April 2008 04:45 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  8
Joined  04-14-2007

After playing a while, I think this would work (combining the the form_dropdown and your code):

if (! function_exists('form_optgroup'))
{
    
function form_optgroup($name = '', $options = array(), $selected = array(), $extra = '')
    
{
        
if ( ! is_array($selected))
        
{
            $selected
= array($selected);
        
}

        
if ($extra != '') $extra = ' '.$extra;

        
$multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

        
$form = '<select name="'.$name.'"'.$extra.$multiple.">\n";

        foreach (
$options as $key => $val)
        
{
      
if(is_array($val))
      
{
        $form
.= '<optgroup label="'.$key.'">'."\n";
        foreach (
$val as $key2 => $val2)
        
{

                $sel
= (in_array($key2, $selected))?' selected="selected"':'';
          
$form .= '<option value="'.$key2.'"'.$sel.'>'.$val2."</option>\n";
        
}
        $form
.= "</optgroup>\n";
      
} else
      
{
              $key
= (string) $key;
              
$val = (string) $val;

              
$sel = (in_array($key, $selected))?' selected="selected"':'';

              
$form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
      
}
        }

        $form
.= '</select>';

        return
$form;
    
}
}
Profile
 
 
Posted: 10 November 2009 05:35 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  2
Joined  06-07-2008

Thanks a lot r.tahara , you function works perfectly !, regards.

Less code, in the moment when you need to retrieve the results, in my case, I just used:

in my controller:
foreach($query_sql->result() as $row)
  {
      $a_array[$row->category][$row->id] = $row->titulo;
  }
$id_areas_estudios = $a_array;
———————————————————
and in my view :
echo form_optgroup(‘id_area_estudio’, $id_areas_estudios);

this produce:

<select name=“id_area_estudio”>
<optgroup label=“Comunicacion y Ciencias sociales”>
<option value=“1”>Antropología</option>
<option value=“2”>Ciencias Políticas</option>
<option value=“3”>Comunicación Audiovisual</option>
<option value=“4”>Comunicación Social</option>
<option value=“5”>Intérprete</option>
<option value=“6”>Periodismo</option>
<option value=“7”>Publicidad</option>
<option value=“8”>Relaciones Internacionales</option>
<option value=“9”>Relaciones Públicas</option>
<option value=“10”>Sociología</option>
<option value=“11”>Trabajo Social</option>
<option value=“12”>Traducción</option>
</optgroup>
</select>

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 115028 Total Logged-in Users: 63
Total Topics: 122469 Total Anonymous Users: 4
Total Replies: 647386 Total Guests: 466
Total Posts: 769855    
Members ( View Memberlist )