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 :

boucle FOR ou FOREACH [PHP 5.3]


Sujet :

Langage PHP

  1. #1
    Débutant Avatar de ETVigan
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Avril 2010
    Messages
    660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2010
    Messages : 660
    Points : 170
    Points
    170
    Par défaut boucle FOR ou FOREACH
    Souci de ré-écriture d'un script et remplacement d'une boucle FOR par un FOREACH où j'ai un souci...
    Voici le message PHP
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    [04-Aug-2012 10:15:30] PHP   2. user_error() F:\WebSites\t1\index.php:218
    
    [04-Aug-2012 10:15:30] PHP Catchable fatal error:  Object of class String could not be converted to string in F:\WebSites\t1\index.php on line 75
    
    [04-Aug-2012 10:15:30] PHP Stack trace:
    Et le coding ou la partie A est remplacée par B
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    				print("Form Validate[".$i."/".count($this->form)."]" . CRLF) ;
    				print_r($this->form) ;
    PARTIE B
    				foreach($this->form as $key => $value)
    75 ==>				{	    echo "Key: $key; Value: $value<br />\n";
    //						print("Form Validate[".$key."/".$value."]" . CRLF) ;
    //					$this->form[$i]->validateField() ;
    				}
    /*1 PARTIE A
    				for($i = 0 ; $i < $this->cnt ; $i++) 
    				{	print("Form Validate[".$i."/".$this->cnt."]" . CRLF) ;
    					$this->form[$i]->validateField() ;
    				}		
    */
    Le print_r donne ceci!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Form Validate[0/2]
    Array ( [0] => String Object ( [fldName:String:private] => prenom [fldMust:String:private] => MUST [fldType:String:private] => [fldError:String:private] => [fldMinl:String:private] => [fldMaxl:String:private] => ) [1] => String Object ( [fldName:String:private] => nom [fldMust:String:private] => MUST [fldType:String:private] => [fldError:String:private] => [fldMinl:String:private] => [fldMaxl:String:private] => ) )
    Ce qui signifie pour moi que les 2 objets sont bien là (Form Validate[0/2] mais que leurs propriétés sont PRIVATES
    Est ce mon problème ?

    J'ai remplacé le PRIVATE par PROTECTED dans le constucteur de la class STRING mais cela ne change rien...
    Code : 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
    		class String //1 implements MethodList
    		{	protected $fldName, $fldMust, $fldType, $fldError, $fldMinl, $fldMaxl ;
    
    			function __construct($fldName, $fldMust, $fldType, $fldError, $fldMinl, $fldMaxl)
    			{	$this->fldName  = $fldName  ;
    				$this->fldMust  = $fldMust  ;
    				$this->fldName  = $fldName  ;
    				$this->fldName  = $fldName  ;
    				$this->fldName  = $fldName  ;
    				$this->fldName  = $fldName  ;
    
    				print("String[__construct][$fldName]" . CRLF ) ;
    			}
    				
    /**
    * 	STRING: initField
    *
    */			function initField()
    			{   print("String[initField]" . CRLF) ;
    			}
    			
    /**
    * 	FORM: validateField
    *
    */			
    			function validateField()
    			{   $val = true ;
    			
    				print("String[validateField]" . CRLF) ;
    				return($val) ;
    			}
    				
    /**
    * 	STRING: isFilled
    *
    */
    			function isFilled()
    			{ 	$fi = (isset( $_POST["$this->fldName"] ) AND ($_POST["$this->fldName"] != NULL) );
    				print("String[isFilled][$fi]" . CRLF) ;
    				return( $fi ) ; 
    			}
    		}
    Merci de m'éclairer....
    Bon WE

    J'ai effectué un changement qui m'interpelle car me confond...
    La boucle fonctionne mais plus le PRINT_T

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    				print_r($this->form . CRLF) ;
    				foreach($this->form as $key => $value)
    				{	  echo "Key: $key<br />\n";
    //						print("Form Validate[".$key."/".$value."]" . CRLF) ;
    //					$this->form[$i]->validateField() ;
    				}
    Ce qui donne pour les prints:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Form Validate[0/2]
    Array    <== PLUS RIEN ICI
    Key: 0
    Key: 1
    FORM OK
    Esteban

  2. #2
    Membre éprouvé

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    748
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 748
    Points : 1 022
    Points
    1 022
    Par défaut
    Peu être comme ca ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    foreach($this->form  as $id ){	
    foreach($id as $key => $value){	
        echo "Key: $key; Value: $value<br />\n;
    }
    }
    Conception / Dev

  3. #3
    Débutant Avatar de ETVigan
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Avril 2010
    Messages
    660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2010
    Messages : 660
    Points : 170
    Points
    170
    Par défaut Autre question
    Merci de ta réponse,
    Je suis entrain de tester le foreach que tu proposes... un peu comme un tableau à 2 dimensions ce qui n'est pas totalement faux...
    La 1ère colonne est le n° de l'objet
    La 2ieme l'objet lui même

    Pour le moment, cela donne ce que je souhaite - après tâtonnement comme ceci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    print("Form[validateForm]" . CRLF) ;
    print_r($this->form . CRLF) ;
    foreach($this->form as $key => $value)
    {	print("Form Validate[".$i++."/".count($this->form)."]" . CRLF) ;
    	echo "Key: $key<br />\n";
    	$value->validateField() ;
    }
    MAIS JE N'AI TJS RIEN AU NIVEAU DU PRINT_T

    Merci encore
    Esteban

  4. #4
    Membre éprouvé

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    748
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 748
    Points : 1 022
    Points
    1 022
    Par défaut
    Je n'ai pas vu ou tu fais apparaitre la variable $i; mais si comme tu le propose elle existe , il suffit de faire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    print_r($this->form[$i]);
    foreach($this->form[$i] as $key => $value)

    Note : à non, tu fais $i++, donc ca marchera pas, mais tu peu imbriquer le for, et le foreach, ou utiliser les deux foreach... ) pour avoir foreach($this->form[$i] as $key => $value)


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    for($i = 0 ; $i <count($this->form) ; $i++) {
    foreach($this->form[$i] as $key => $value){}
    }
    Conception / Dev

  5. #5
    Débutant Avatar de ETVigan
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Avril 2010
    Messages
    660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2010
    Messages : 660
    Points : 170
    Points
    170
    Par défaut variable $I
    $i était utilisé dans l'ancienne écriture

    Code : 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
    			function validateForm()
    			{   $i = 0 ;
    				
    				print("Form[validateForm]" . CRLF) ;
    				print_r($this->form . CRLF) ;
    				foreach($this->form as $key => $value)
    				{	    print("Form Validate[".$i++."/".count($this->form)."]" . CRLF) ;
    						echo "Key: $key<br />\n";
    						$value->validateField() ;
    				}
    /*1   ANCIEN
    				for($i = 0 ; $i < $this->cnt ; $i++) 
    				{	print("Form Validate[".$i."/".$this->cnt."]" . CRLF) ;
    					$this->form[$i]->validateField() ;
    				}		
    */
    				return($this->isFormValid()) ;
    			}
    Elle n’apparaît que dans la nouvelle que pour raison de traçage...

    Merci
    Esteban

  6. #6
    Débutant Avatar de ETVigan
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Avril 2010
    Messages
    660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2010
    Messages : 660
    Points : 170
    Points
    170
    Par défaut Autre question
    Juste une question, dans la fonction initField, je souhaiterai remettre la value d'un champ à NULL et non l'instance de ce champ.

    Je ne vois que la solution de remettre le $_POST[du champ en question] à null

    Je pense que cette pratique n'est pas orthodoxe mais n'en voit pas une autre...

    Voici mon coding:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    */			function initField()
    			{   if ($this->getDebug())
    					print("String[initField]" . CRLF) ;
    				
    				print_r($_POST . CRLF) ;
    				echo  "Before ===>" . $this->fldName . $_POST[".$this->fldName."] . CRLF ;
    				$_POST["$this->fldName"] = NULL ;
    				echo  "After  ===>" . $this->fldName . $_POST["$this->fldName"] . CRLF ;
    			}
    Remarque; le print_r du $_post ne donne rien pourtant je passe par là puisque String[initField] est affiché...

    Merci
    Esteban

  7. #7
    Membre éprouvé

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    748
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 748
    Points : 1 022
    Points
    1 022
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    $post = isset($_POST[$this->fldName]) ?$_POST :  array( $this->fldName =>null);//opérateur ternaire
    print_r( $post ) ;
    echo  CRLF ;
     
    echo  "Before ===>" . $this->fldName .$post [$this->fldName] . CRLF ;
    $post [$this->fldName] = NULL ;
    echo  "After  ===>" . $this->fldName .$post [$this->fldName] . CRLF ;
    tu ne peu pas changer directement la valeur d'une variable prédéfini tel que POST , donc tu passe par $post = isset($_POST[$this->fldName])) ? $_POST : array();


    Note : isset($_POST) retourn toujours 1; donc il faut bien vérifier l'existance d'une clé dans $_POST, et non l'existance simple de $_POST
    Conception / Dev

  8. #8
    Débutant Avatar de ETVigan
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Avril 2010
    Messages
    660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2010
    Messages : 660
    Points : 170
    Points
    170
    Par défaut $_POST
    Je ne comprends pas trop...
    Voici les impressions à mon écran

    Code : 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
     
    Prénom EMILIE Nom MOLINER (Prénom, nom rajoutés à la main.... )
     
     
    _POST - reset
    Form initForm[0]
    Key: 0
    String[initField]
    Array
    Before ===>prenom
    After ===>prenomemilie
    Form initForm[1]
    Key: 1
    String[initField]
    Array
    Before ===>nom
    After ===>nommoliner
    FORM reset
    No submit
    Mon coding avec la ligne que tu as modifié...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    */			function initField()
    			{   if ($this->getDebug())
    					print("String[initField]" . CRLF) ;
    				
    				print_r($_POST . CRLF) ;
    				echo  "Before ===>" . $this->fldName . $_POST[".$this->fldName."] . CRLF ;
     				$post = (isset($_POST[$this->fldName])) ? $_POST : array(); 
     				echo  "After  ===>" . $this->fldName . $_POST["$this->fldName"] . CRLF ;
    			}
    Note la différence entre Before/After dans les displays ci-dessus, la valeur est affichée dans l'after... après la RAZ ????

    Pige plus

    Encore merci
    Esteban

  9. #9
    Membre éprouvé

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    748
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 748
    Points : 1 022
    Points
    1 022
    Par défaut
    faut vraiment que tu copy colle l'integralité du code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
     
    function initField()
    {  
     if ($this->getDebug()){
    $post = isset($_POST[$this->fldName]) ?$_POST :  array( $this->fldName =>null);//opérateur ternaire
    print_r( $post ) ;
    echo  CRLF ;
    echo  "Before ===>" . $this->fldName .$post [$this->fldName] . CRLF ;
    $post [$this->fldName] = NULL ;
    echo  "After  ===>" . $this->fldName .$post [$this->fldName] . CRLF ;
    			}
    }
    Conception / Dev

  10. #10
    Débutant Avatar de ETVigan
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Avril 2010
    Messages
    660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2010
    Messages : 660
    Points : 170
    Points
    170
    Par défaut $_POST
    Reste la bouteille à encre pour moi....
    Désolé !
    Je pense avoir copier/Coller...pourtant !

    ATTENDS non je n'ai pas bien regarder...
    JE CONTINUE

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    _POST - reset
    Form initForm[0]
    Key: 0
    String[initField]
    Array
    Before ===>prenom
    After ===>prenomemilie
    Form initForm[1]
    Key: 1
    String[initField]
    Array
    Before ===>nom
    After ===>nommoliner
    FORM reset
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    */			function initField()
    			{   if ($this->getDebug())
    					print("String[initField]" . CRLF) ;
    				
    				print_r($_POST . CRLF) ;
    				echo  "Before ===>" . $this->fldName . $_POST[".$this->fldName."] . CRLF ;
     				$post = (isset($_POST[$this->fldName])) ? $_POST : array( $this->fldName =>null); 
     				echo  "After  ===>" . $this->fldName . $_POST["$this->fldName"] . CRLF ;
    			}
    Merci
    Esteban

  11. #11
    Membre éprouvé

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    748
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 748
    Points : 1 022
    Points
    1 022
    Par défaut
    Ton code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    function initField()
    			{   if ($this->getDebug())
    					print("String[initField]" . CRLF) ;
     
    				print_r($_POST . CRLF) ;
    				echo  "Before ===>" . $this->fldName . $_POST[".$this->fldName."] . CRLF ;
     				$post = (isset($_POST[$this->fldName])) ? $_POST : array( $this->fldName =>null); 
     				echo  "After  ===>" . $this->fldName . $_POST["$this->fldName"] . CRLF ;
    			}

    Le miens


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    function initField()
    {  
     if ($this->getDebug()){
    $post = isset($_POST[$this->fldName]) ?$_POST :  array( $this->fldName =>null);//opérateur ternaire
    print_r( $post ) ;
    echo  CRLF ;
    echo  "Before ===>" . $this->fldName .$post [$this->fldName] . CRLF ;
    $post [$this->fldName] = NULL ;
    echo  "After  ===>" . $this->fldName .$post [$this->fldName] . CRLF ;
    			}
    }

    C'est po trop le même...;
    Conception / Dev

  12. #12
    Membre éprouvé

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    748
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 748
    Points : 1 022
    Points
    1 022
    Par défaut
    //$_POST[$this->fldName]//fonctionne toujours
    //$_POST[".$this->fldName."] //devrait pas fonctionner
    //$_POST["$this->fldName"] //devrait pas fonctionner
    Conception / Dev

  13. #13
    Débutant Avatar de ETVigan
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Avril 2010
    Messages
    660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2010
    Messages : 660
    Points : 170
    Points
    170
    Par défaut $_POST
    Maintenant il est similaire...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    			function initField()
    			{   if ($this->getDebug())
    					print("String[initField]" . CRLF) ;
    $post = isset($_POST[$this->fldName]) ?$_POST :  array( $this->fldName =>null);//opérateur ternaire
    print_r( $post ) ;
    echo  CRLF ;
    echo  "Before ===>" . $this->fldName .$post [$this->fldName] . CRLF ;
    $post [$this->fldName] = NULL ;
    echo  "After  ===>" . $this->fldName .$post [$this->fldName] . CRLF ;
     
    			}
    Mais après avoir fait un tour de moulinette...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
    _POST - reset
    Form initForm[0]
    Key: 0
    String[initField]
    Array ( [textarea] => [prenom] => emilie [nom] => moliner [RESET] => Effacer [message] => Prêt â recevoir l'encodade de votre formulaire )
    Before ===>prenomemilie
    After ===>prenom
    Form initForm[1]
    Key: 1
    String[initField]
    Array ( [textarea] => [prenom] => emilie [nom] => moliner [RESET] => Effacer [message] => Prêt â recevoir l'encodade de votre formulaire )
    Before ===>nommoliner
    After ===>nom
    FORM reset
    OK les valeurs ont changés dans l'after mais pas à l'écran !!!

    Merci
    Esteban

  14. #14
    Membre éprouvé

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    748
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 748
    Points : 1 022
    Points
    1 022
    Par défaut
    il faut maintenant que tu utilise la variable $post qui a été modifié, et non $_POST qui restera toujours à la valeur que tu a rentré dans le formulaire ( tu ne changera jamais la valeur de $_POST['prenom'], c'est impossible ), et c'est pour cela que tu passe par une variable intemédiaire
    Conception / Dev

  15. #15
    Débutant Avatar de ETVigan
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Avril 2010
    Messages
    660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2010
    Messages : 660
    Points : 170
    Points
    170
    Par défaut $_POST
    Cela semble enfin ok
    Donc tu as si j'ai bien compris, tu as duplicié le $_POST en $post
    Cette toute une autre organisation du formulaire cela... non ?

    Suis je tellement tordu ou tout le monde procède ainsi ?

    Merci d'avoir été patient...
    Esteban

  16. #16
    Membre éprouvé

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    748
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 748
    Points : 1 022
    Points
    1 022
    Par défaut
    Je pense que tout ceux qui veulent que cela fonctionne procèdent ainsi...
    Conception / Dev

  17. #17
    Membre éprouvé

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    748
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 748
    Points : 1 022
    Points
    1 022
    Par défaut
    j'avais donné un bout de code, pour filter les values d'un post

    http://www.developpez.net/forums/d12...me-formulaire/

    Code : 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
    <?php
    class FilterTest
    {
     
       public static function _call($a,$b)
        {
     
        $string = self::check_space($a);
     
             switch ($b){
     
            case "STRING_IDENT":
     
                if(strlen($string)<3 || strlen($string)>10 ){
                    $string = false;
                }
             break;
             }
     
        return $string;
        }
        public static function check_space($string){
            $temp=trim($string);
            $string="";
            for ( $i = 1 ; $i < strlen($temp) +1 ; $i ++ ){
                if ( $temp[$i-1]==" " &&  $temp[$i]==" "){
                }
                else{
                $string.= $temp[$i-1] ;
                }
            }
            return $string;
        }
     
    }
     
    $filtres = array(
        'Identifiant' => array(
        'filter' => FILTER_CALLBACK,
        'options' => create_function('$a','return FilterTest::_call($a,"STRING_IDENT");') 
        ),
        'Password' => array (
        'filter' => FILTER_CALLBACK,  
        'options' => create_function('$a','return FilterTest::_call($a,"STRING_PASSWORD");') 
        ),
        'Email' => FILTER_VALIDATE_EMAIL 
    );
     
     
    $resultat = filter_input_array(INPUT_POST, $filtres); 
    echo strlen($resultat['Identifiant']).'<br/>';
    print_r($resultat);


    si ca peu te donner une autre approche d'un traitement de formulaire
    Conception / Dev

  18. #18
    Débutant Avatar de ETVigan
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Avril 2010
    Messages
    660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2010
    Messages : 660
    Points : 170
    Points
    170
    Par défaut Autre question
    Puis-je encore poser une question ?
    J'ai ma forme en POST avec deux boutons interceptés par $_POST
    Les 2 boutons se nomment SUBMIT & RESET en gras...
    Code : 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
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="CheckForm">
    <table width="615" border="0">
      <tr>
        <td>&nbsp;</td>
        <td colspan="4"><div align="center"><b><?php echo $cf->getTitle() ?></b></div></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="6"><hr /></td>
      </tr>
      <?php
    		if (!$cf->isFormValid())
    			{
      ?>
      <tr>
        <td height="104">&nbsp;</td>
        <td colspan="4" align="center">
            <textarea name="textarea" cols="70" rows="5"><?php echo $cf->getStackError()?></textarea>   
    	</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="6"><hr /></td>
      </tr>
      <?php
      			}
      ?>
      <tr>
        <td width="60">Prénom</td>
        <td width="130">
    		<input type="text" name="prenom" maxlength="32" value="<?php if (isset($post['prenom'])) echo $post['prenom'] ?>"/>	</td>
        <td width="127"><div align="center">Nom</div></td>
        <td width="149"><input type="text" name="nom"   maxlength="32" value="<?php if (isset($post['nom'])) echo $post['nom'] ?>" /></td>
        <td width="46">&nbsp;</td>
        <td width="62">&nbsp;</td>
      </tr>
      <tr>
        <td colspan="6"><hr/></td>
      </tr>
      <tr>
        <td><input type="submit"  name="RESET"   value="Effacer"/></td>
        <td colspan="4" align="center">
          	<input type="text"   size=60 name="message"   value="<?php if (isset($cf)) echo $cf->getMsg() ?>" />    
        </td>
        <td><input type="submit" name="SUBMIT" value="Envoyer"/></td>
      </tr>
    </table>
    </form>
    J'écris le code
    Code : 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
    	foreach ($_POST as $key => $value) {
    		echo "key: " . $key . " ==> " . $value . CRLF ;
    	}	
    	
       	if (isset($_POST['RESET']))
    		{	if ($cf->getDebug())
    				{	print("_POST - reset" . CRLF) ;
    					user_error("FORM _POST !" , E_USER_NOTICE) ;
    				}
    /*1
    			$cf->initForm() ;
    			user_error("end _POST - reset" , E_USER_NOTICE) ;
    			print("end _POST - reset" . CRLF) ;
    */
    		}
    	else
    		print("No FORM reset" . CRLF) ;
    		
    		
    /**
    * 	Event[SUBMIT] definition
    *
    */
       	if (isset($_POST['SUBMIT']))
    		{	if ($cf->getDebug())
    				{	print("_POST - submit" . CRLF) ;
    					user_error("FORM submit !" , E_USER_NOTICE) ;
    				}
    /*1
    			if ($cf->validateForm())
    				{	# Formulaire valide
    					user_error("FORM OK !" , E_USER_NOTICE) ;
    					print("FORM OK" . CRLF) ;
    					# Instruction ...
    				}
    			else 
    				{	# Sinon ...
    					user_error("FORM KO !" , E_USER_NOTICE) ;
    					print("FORM KO" . CRLF) ;
    					# Instruction ...
    				}
    			Stack::printStack() ;
    */
    			print("end _POST - submit" . CRLF) ;
    		}
    	else
    		print("No FORM submit" . CRLF) ;
    Ce code imprime:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    key: textarea ==>
    key: prenom ==>
    key: nom ==>
    key: message ==>
    key: SUBMIT ==> Envoyer
    No FORM reset
    L'entrés Key : RESET ==> Effacer n’apparaît... comme espéré et attendu...
    Une idée...

    Mon but, je complète:

    Code : 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
    	function eventReset() {
    //		if ($cf->getDebug())
    			{	print("_POST - reset" . CRLF) ;
    				user_error("FORM _POST RESET!" , E_USER_NOTICE) ;
    			}
    	}
    	
    	function eventSubmit() {
    //		if ($cf->getDebug())
    			{	print("_POST - submit" . CRLF) ;
    				user_error("FORM _POST SUBMIT	!" , E_USER_NOTICE) ;
    			}
    	}
    		
    	$event = array(	"RESET"   => "eventReset"   ,
    		           	"SUBMIT"  => "eventSubmit"
    				   ) ;
    				   
    	foreach ($_POST as $key => $value) {
    		echo "key: " . $key . " ==> " . $value . CRLF ;
    		foreach ($event as $e => $fct) {
    //			echo "key: " . $key . " ==> " . $value . CRLF ;
    			echo "e: " . $e   . " ==> " . $key   . CRLF ;
    			if (strcmp($e,$key) == 0) {
    				echo "Matching[".$e."] - [".$fct."]"  . CRLF ;
    				call_user_func($fct);
    			}
    		}
    	}
    Qui imprime
    Code : 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
    key: textarea ==>
    e: RESET ==> textarea
    e: SUBMIT ==> textarea
    key: prenom ==>
    e: RESET ==> prenom
    e: SUBMIT ==> prenom
    key: nom ==>
    e: RESET ==> nom
    e: SUBMIT ==> nom
    key: message ==>
    e: RESET ==> message
    e: SUBMIT ==> message
    key: SUBMIT ==> Envoyer
    e: RESET ==> SUBMIT
    e: SUBMIT ==> SUBMIT
    Matching[SUBMIT] - [eventSubmit]
    Mais tjs pas de trace de "RESET"

    Merci bonne fin de WE
    Merci.
    Esteban

  19. #19
    Membre éprouvé

    Profil pro
    Inscrit en
    Juin 2007
    Messages
    748
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 748
    Points : 1 022
    Points
    1 022
    Par défaut
    L'entrés Key : RESET ==> Effacer n’apparaît... comme espéré et attendu...
    Une idée...
    key: textarea ==>
    key: prenom ==>
    key: nom ==>
    key: message ==>
    key: SUBMIT ==> Envoyer
    No FORM reset
    Pour moi le code se comporte comme il faut , donc je ne vois pas où est le problème...

    tu as rempli le formulaire où pas, tu as cliqué sur envoyer, tu as le résultat voulu.
    Conception / Dev

  20. #20
    Débutant Avatar de ETVigan
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Avril 2010
    Messages
    660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2010
    Messages : 660
    Points : 170
    Points
    170
    Par défaut Non pas le bon résultat
    Non, je n'ai pas le bon résultat
    A partir du moment ou le bouton RESET est de type SUBMIT il doit apparaitre dans le tableau $_POST sauf grosse erreur de ma part !

    Mais là je suis 99% sur de moi....

    Mais 1% que je ne me trompe.... mais RESET === SUBMIT et donc doit apparaitre !

    Merci @toi
    Esteban

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

Discussions similaires

  1. [Objective-C] boucle for (foreach)
    Par moshehayun dans le forum Objective-C
    Réponses: 3
    Dernier message: 12/05/2011, 10h49
  2. [PHP 5.0] boucle for dans foreach
    Par stomerfull dans le forum Langage
    Réponses: 6
    Dernier message: 15/01/2010, 11h57
  3. Réponses: 18
    Dernier message: 25/07/2007, 12h34
  4. [Swing][boucles] for, do, if .....comment faire simple?
    Par chastel dans le forum AWT/Swing
    Réponses: 7
    Dernier message: 02/05/2004, 22h49
  5. [langage] boucle "for" modification du pas
    Par K-ZimiR dans le forum Langage
    Réponses: 4
    Dernier message: 29/04/2004, 11h54

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