IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Classes pour listes d'options


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Homme Profil pro
    Ingénieur en électrotechnique retraité
    Inscrit en
    Décembre 2008
    Messages
    1 718
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 73
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur en électrotechnique retraité

    Informations forums :
    Inscription : Décembre 2008
    Messages : 1 718
    Par défaut Classes pour listes d'options
    Bonjour à tous,

    J'ai deux classes nommées optListSelect et optListOption. Ces deux classes sont très pratiques pour le traitement automatique des listes d'options constituées en php à partir de bases de données.
    Je voudrais créer une troisième classe optListGroup mais je suis dans le flou total sur la façon de commencer et d'articuler cette classe avec les deux autres.
    Voici le code des deux classes existantes.
    Classe optListSelect:
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    <?php
     
    /**
     * @property-read array optListOption::options
     * @property string $name
     * @property string|int $id
     * @property boolean $disabled
     * @property-read optListOption::selected
     */
    class optListSelect {
    	private $options = [];
    	private $name = '';
    	private $id = '';
    	private $disabled = false;
     
    	public function get_name() {
    		return $this->name;
    	}
    	public function set_name($val) {
    		if(!is_string($val)) throw new InvalidArgumentException("Invalid argument for name attribute of <select> tag. String expected");
    		$this->name = $val;
    	}
    	public function unset_name() {
    		$this->name = '';
    	}
     
    	public function get_id() {
    		return $this->id;
    	}
    	public function set_id($val) {
    		if(!(is_string($val) or is_int($val))) throw new InvalidArgumentException("Invalid argument for id attribute of <select> tag. String or int expected");
    		$this->id = $val;
    	}
    	public function unset_id() {
    		$this->id = '';
    	}
     
    	public function get_disabled() {
    		return $this->disabled;
    	}
    	public function set_disabled($val) {
    		$this->disabled = (bool)$val;
    	}
    	public function unset_disabled($val) {
    		$this->disabled = false;
    	}
     
    	public function get_selected() {
    		$return = null;
    		foreach($this->options as $option) {
    			if($option->get_selected()) {
    				//$return = $option;
    				$return = ['value'=>$option->get_value(), 'text'=>$option->get_text()];
    				break;
    			}
    		}
    		return $return;
    	}
     
    	public function get_options() {
    		return $this->options;
    	}
    	public function addOption(optListOption $option) {
    		$this->options[] = $option;
    	}
     
    	public function __toString() {
    		// ordre des paramètres: name, id, disabled, options
    		return sprintf(
    			'<select%s%s%s>%s</select>',
    			empty($this->name) ? '' : sprintf(' name="%s"', $this->name),
    			empty($this->id) ? '' : sprintf(' id="%s"', $this->id),
    			$this->disabled ? ' disabled="disabled"' : '',
    			implode('', $this->options)
    		);
    	}
     
    	public function __construct($name, $id='', $disabled = false)
    	{
    		if (!$name) throw new BadMethodCallException("Invalid call for ".__METHOD__. ". Missing parameter");
    		$this->set_name($name);
    		if (!$id) $id = $name;
    		$this->set_id($id);
    		$this->set_disabled($disabled);
    	}
     
    }
    Classe optListOption:
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    <?php
     
    /**
     * Classe décrivant une option de liste de sélection
     * @property mixed $value Valeur de l'option (attribut html value)
     * @property string $text Texte à afficher pour l'option.
     * @property boolean $selected L'option est sélectionnée ou non.
     */
    class optListOption {
    	private $value = null;
    	private $text;
    	private $selected = false;
     
    	public function get_value() {
    		return $this->value;
    	}
    	public function set_value($value) {
    		if(!(is_null($value) or is_string($value) or is_int($value))) throw new InvalidArgumentException("Invalid typ for the value of the option");
    		$this->value = $value;
    	}
    	public function unset_value() {
    		$this->value = null;
    	}
     
    	public function get_text() {
    		return $this->text;
    	}
    	public function set_text($value) {
    		if(!(is_null($value) or is_string($value) or is_int($value))) throw new InvalidArgumentException("Invalid typ for the text of the option");
    		$this->text = $value;
    	}
     
    	public function get_selected() {
    		return $this->selected;
    	}
    	public function set_selected($val) {
    		$this->selected = (bool)$val;
    	}
     
    	public function __construct($text = '', $value = null, $selected = false) {
     
    		$this->set_value($value);
    		$this->set_text($text);
    		$this->set_selected($selected);
    	}
     
    	public function __toString() {
    		return sprintf(
    			'<option%s%s>%s</option>',
    			is_null($this->value) ? '' : sprintf(' value="%s"', $this->value),
    			$this->selected ? ' selected="selected"' : '',
    			$this->text
    		);
    	}
    }

  2. #2
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Par défaut
    Une optgroup est une liste d'options et il peut y avoir une liste d'Optgroup dans une liste :
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <select name="c">
          <optgroup label="8.01 Physics I: Classical Mechanics">
            <option value="8.01.1">Lecture 01: Powers of Ten</option>
            <option value="8.01.2">Lecture 02: 1D Kinematics</option>
            <option value="8.01.3">Lecture 03: Vectors</option>
          </optgroup>
          <optgroup label="8.02 Electricity and Magnestism">
            <option value="8.02.1">Lecture 01: What holds our world together?</option>
            <option value="8.02.2">Lecture 02: Electric Field</option>
            <option value="8.02.3">Lecture 03: Electric Flux</option>
          </optgroup>
          <optgroup label="8.03 Physics III: Vibrations and Waves">
            <option value="8.03.1">Lecture 01: Periodic Phenomenon</option>
            <option value="8.03.2">Lecture 02: Beats</option>
            <option value="8.03.3">Lecture 03: Forced Oscillations with Damping</option>
          </optgroup>
        </select>
    Donc la structure de ta classe optListOptGroup va ressembler beaucoup à celle de optListSelect : une liste d'options, un label et un flag disabled, et s'ajouter dans optListSelect de la même façon sur les options.
    Modératrice PHP
    Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)
    Cherchez un peu avant poser votre question : Cours et Tutoriels PHP - FAQ PHP - PDO une soupe et au lit !.

    Affichez votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur) et [C=php][/C]

  3. #3
    Membre éprouvé
    Homme Profil pro
    Ingénieur en électrotechnique retraité
    Inscrit en
    Décembre 2008
    Messages
    1 718
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 73
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur en électrotechnique retraité

    Informations forums :
    Inscription : Décembre 2008
    Messages : 1 718
    Par défaut
    Si je comprends bien ton idée, dans mon optListSelect, je pourrais ajouter soit une option soit un groupe qui lui même contiendrait des options.

  4. #4
    Membre éprouvé
    Homme Profil pro
    Ingénieur en électrotechnique retraité
    Inscrit en
    Décembre 2008
    Messages
    1 718
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 73
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur en électrotechnique retraité

    Informations forums :
    Inscription : Décembre 2008
    Messages : 1 718
    Par défaut
    Selon tes indications et après quelques modifications mineures, tout fonctionne. Merci!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 14/03/2016, 10h51
  2. [Python 2.X] Creation d'un classe pour incrémenter une liste
    Par nekcorp dans le forum Général Python
    Réponses: 9
    Dernier message: 13/02/2015, 13h40
  3. Réponses: 7
    Dernier message: 04/05/2010, 18h31
  4. Classe pour la création d'un graphe xy
    Par Bob dans le forum MFC
    Réponses: 24
    Dernier message: 03/12/2009, 17h20
  5. Réponses: 8
    Dernier message: 08/05/2004, 13h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo