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 :

Invalid argument?


Sujet :

Langage PHP

  1. #1
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut Invalid argument?
    Salut,

    Dnas ma classe j'appelle une fonction add_template() avec un argument:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $template->add_template(array('style.css', 'head_big_table.tpl'));
    Je récupère ensuite la variable transmise et la traite avec foreach:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    function add_template($template) {
    		foreach($template As $id => $file) {
    /* ... */
    Le problème: c'est qu'il me renvoit une erreur comme quoi
    Warning: Invalid argument supplied for foreach() in c:\program files\easyphp1-8\www\plan\cls.inc.php on line 41
    A quoi est-ce du?

  2. #2
    Rédacteur
    Avatar de marcha
    Homme Profil pro
    Développeur Web
    Inscrit en
    Décembre 2003
    Messages
    1 571
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Décembre 2003
    Messages : 1 571
    Points : 2 351
    Points
    2 351
    Par défaut
    Salut,

    En général c'est du à la tentative d'itérer une variable qui n'est pas
    un tableau.

    assure-toi que $template est bien un tableau, par exemple en
    testant avec is_array()
    A+
    Marc
    Si ton code fait plus d'une ligne, c'est que tu as mal choisi ton langage !

  3. #3
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    Salut,

    J'ai testé avec print_r($template) et aucune problème il m'affiche bien: Array(1 => valeur, 2 => valeur)...


  4. #4
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    essaie plutot avec as et non As

  5. #5
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    non

  6. #6
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    Voila j'essai ça tout court ça fonctionne:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    		foreach($template as $id => $file) {
    			echo $id." => ".$file;
    		}
    		exit;
    Donc je pense que c'est dans mon foreach qui plante, et là en l'occurence mon switch() :

    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
    		foreach($template as $id => $file) {
    			$open = fopen('templates/'.$file, 'r');
    			$read = fread($open, filesize('templates/'.$file));
    			fclose($open);
     
    			switch($file) {
    				case 'style.css':
    					$read = '<link rel="stylesheet" href="templates/style.css"';
    					break;
    				case 'block.tpl':
    					$read = str_replace('[!CONTENT!]', $this->special_var, $read);
    					break;
    				default: break;
    			}
     
    			echo $read;
    		}
    ?

  7. #7
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    pourquoi non ?

    Donnes-nous un peu plus de code

  8. #8
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    Ah double post désolé
    Regarde au dessus. Le non voulai dire ça ne fonctionne pas.

  9. #9
    Membre chevronné
    Avatar de ska_root
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2005
    Messages
    1 203
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Service public

    Informations forums :
    Inscription : Août 2005
    Messages : 1 203
    Points : 1 839
    Points
    1 839
    Par défaut
    Salut,

    ben ça devrait marcher ton truc...
    commentes ta boucle et affiches le paramètre avec un var_dump par exemple, histoire de voir si tu as bien passé un array

    si c'est le cas, c'est que tu n'exploites pas comme il faut la boucle foreach...
    exemple ( au cas ou ) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    function add_template($template) {
       if(is_array($template)) {
          echo "array(<br />";
          foreach ( $template As $key => $item ) {
             echo "   [$key] => $item<br />\n";
          }
          echo ")<br />\n";
       }else echo "attention le paramètre n'est pas un array !";
    }
     
    add_template(array('val1' => 1, 'val2' => 2, 'val3' => 3));
    [EDIT]
    zut, pas assez rapide petit scarabée...
    oublies ce post...

  10. #10
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    Il t'indique quelle ligne pour l'erreur ?
    sinon un commentaire :
    si file == style.css alors tu ouvre et lit un fichier pour rien !

  11. #11
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    C'est bizarre ton code ne m'affiche pas l'erreur et pourtant mon code foire toujours...

    Edit: oui pas bête Mr N. Et il m'affiche cette erreur:
    Warning: Invalid argument supplied for foreach() in c:\program files\easyphp1-8\www\plan\cls.inc.php on line 41
    [EDIT] Euh pourquoi oublie ce post?

  12. #12
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    A mon avis la vérité est alleurs
    Ca se passe pour quelle clef ?
    il est déjà passé dans ton switch ?
    trace l'execution de ta fonction pour voir à partir de quand ca plante...

  13. #13
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    A non mais ça plante tout de suite. Rien n'apparait j'ai le warning impossible d'afficher quelque chose avant.

  14. #14
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    montre nous le code complet de ta fonction

  15. #15
    Membre chevronné
    Avatar de ska_root
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2005
    Messages
    1 203
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Service public

    Informations forums :
    Inscription : Août 2005
    Messages : 1 203
    Points : 1 839
    Points
    1 839
    Par défaut
    Citation Envoyé par Anduriel
    [EDIT] Euh pourquoi oublie ce post?
    parce que j'avais pas vu ton extrait de code, et que je pense comme Mr N., le problème est ailleurs...

  16. #16
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    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
    /* Class Templates */
    class template {
    	var $special_var;
    	var $special_files;
     
    	function template() {
    		$this->special_files = array('style.css');
    	}
     
    	function add_template($template) {
    		foreach($template as $id => $file) {
     
    			if (!in_array($file, $this->special_files)) {
    				$open = fopen('templates/'.$file, 'r');
    				$read = fread($open, filesize('templates/'.$file));
    				fclose($open);
    			}
     
    			switch($file) {
    				case 'style.css':
    					$read = '<link rel="stylesheet" href="templates/style.css"';
    					break;
    				case 'block.tpl':
    					$read = str_replace('[!CONTENT!]', $this->special_var, $read);
    					break;
    				default: break;
    			}
     
    			echo $read;
    		}
    	}
    }
    et l'appel:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $template = New template();
    $template->add_template(array('style.css', 'head_big_table.tpl'));

  17. #17
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    Ca donne quoi ça ?
    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
     
    function add_template($template) {
          echo '<<< add_template'; flush();
          foreach($template as $id => $file) {
             echo $id,' => ',$file; flush();
             if (!in_array($file, $this->special_files)) {
                echo 'Special file !'; flush();
                $open = fopen('templates/'.$file, 'r');
                $read = fread($open, filesize('templates/'.$file));
                fclose($open);
             }
     
             switch($file) {
                case 'style.css':
                   echo 'css !'; flush();
                   $read = '<link rel="stylesheet" href="templates/style.css"';
                   break;
                case 'block.tpl':
                   echo 'tpl !'; flush();
                   $read = str_replace('[!CONTENT!]', $this->special_var, $read);
                   break;
                default: break;
             }
     
             echo $read;
          }
          echo '>>> add_template'; flush();
       }

  18. #18
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    J'ai ça:

    <<< add_template0 => style.csscss ! head_big_table.tplSpecial file !>>> add_template<<< add_template
    Warning: Invalid argument supplied for foreach() in c:\program files\easyphp1-8\www\plan\cls.inc.php on line 47
    >>> add_template
    Et toujours cette erreur Invalid!!

  19. #19
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    On continue : Ca donne quoi ça ?
    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
    function add_template($template) {
          echo "<<< add_template<pre>\n"; flush();
          var_dump($template);
          echo "</pre>"; flush();
          foreach($template as $id => $file) {
             if (!in_array($file, $this->special_files)) {
                $open = fopen('templates/'.$file, 'r');
                $read = fread($open, filesize('templates/'.$file));
                fclose($open);
             }
     
             switch($file) {
                case 'style.css':
                   $read = '<link rel="stylesheet" href="templates/style.css"';
                   break;
                case 'block.tpl':
                   $read = str_replace('[!CONTENT!]', $this->special_var, $read);
                   break;
                default: break;
             }
     
             echo $read;
          }
          echo '>>> add_template'; flush();
       }

  20. #20
    Membre expérimenté
    Avatar de Anduriel
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Février 2004
    Messages
    2 290
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration

    Informations forums :
    Inscription : Février 2004
    Messages : 2 290
    Points : 1 500
    Points
    1 500
    Par défaut
    On continu avec mon erreur ^^

    <<< add_template
    array(2) {
    [0]=>
    string(9) "style.css"
    [1]=>
    string(18) "head_big_table.tpl"
    }

    >>> add_template<<< add_template
    string(9) "block.tpl"


    Warning: Invalid argument supplied for foreach() in c:\program files\easyphp1-8\www\plan\cls.inc.php on line 49
    >>> add_template

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. [IPC] msgsnd : Invalid Argument
    Par KneXtasY dans le forum C
    Réponses: 1
    Dernier message: 31/12/2006, 01h34
  2. Invalid argument supplied for foreach()
    Par Rajhonson dans le forum Langage
    Réponses: 4
    Dernier message: 16/11/2006, 14h31
  3. Réponses: 11
    Dernier message: 19/05/2006, 11h41
  4. Réponses: 26
    Dernier message: 24/03/2006, 13h44
  5. [Tableaux] Problème invalid argument foreach()
    Par kcizth dans le forum Langage
    Réponses: 9
    Dernier message: 05/01/2006, 11h56

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