PHP: Création automatique d’un menu ‘select’ HTML

Author:


Download

<?php
 
function input_select($nom_control, $selected, $options, $multiple = false)
{
    print '
<select name="' . $nom_control;
    // Pour les sélections multiples
    if ($multiple) { print '[]" multiple="multiple'; }
    print '">';
 
    $selected_options = $options;
    if ($multiple) {
        foreach ($selected[$nom_control] as $val) {
            $selected_options[$val] = true;
        }
    }
    else
    {
        $selected_options[ $selected[$nom_control] ] = true;
    }
 
    foreach ($options as $option => $label) {
        print '
<option value="' . htmlentities($option) . '"';
        if ($selected_options[$option])
        {
            print ' selected="selected"';
        }
        print '>' . htmlentities($label) . '</option>
 
';
    }
    print '</select>
 
';
}
 
$pays=array('Paris','Londre','New York','madrid');
$nom_select='pays';
$elem_select=1;
input_select($nom_select,$elem_select,$pays,true);
 
?>

Leave a Reply

Your email address will not be published. Required fields are marked *


× 5 = forty