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

PHP & Base de données Discussion :

framework symfony php


Sujet :

PHP & Base de données

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Février 2015
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2015
    Messages : 8
    Par défaut framework symfony php
    hello, i m a debutant on programming in symfony framework so
    I'm coding up a twig template to display a list of cars in a simple HTML table .here is the erreur, that i don t understand what i have to do :
    the erreur : Key "Id" for array with keys "0, 1, 2" does not exist in MyAppEspritParcBundle:Voiture:list.html.twig at line 20
    Nom : errr.png
Affichages : 965
Taille : 84,4 Ko

    the function listAction in the controller :

    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
        function listAction()
         {
             $marques=array('BMW','Renault','Fiat');
             //list des voitures
             $voiture=array(array('Id'=>'C2345','serie'=>'176','dateMiseCirculation'=>'01/01/2014','Marque'=>'BMW'),
                 array('Id'=>'A2345','serie'=>'186','dateMiseCirculation'=>'13/01/2015','marque'=>'Renault'),
                 array('Id'=>'W0308','serie'=>'196','dateMiseCirculation'=>'02/11/2012','marque'=>'Fiat'));
             return $this->render('MyAppEspritParcBundle:Voiture:list.html.twig',
                     array('marques'=>$marques,'voiture'=>$voiture));
         }

    the twig.html:
    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
    18
    19
    <h2>List des voitures</h2>
    <table border="1">
        <tr>
        <td>Id</td>
        <td>Série</td>
        <td>Date de Mise Circulation</td>
        <td>Marque</td>
       </tr>
       {%for  i in voiture %}
       <tr>
           <td>{{ voiture.Id }}</td>
           <td>{{ voiture.serie }}</td>
           <td>{{ voiture.dateMiseCirculation }}</td>
           <td>{{ voiture.marque }}</td>
     
       </tr>
     
       {% endfor %}
       </table>
    any help ?

  2. #2
    Membre émérite Avatar de tdutrion
    Homme Profil pro
    Architecte technique
    Inscrit en
    Février 2009
    Messages
    561
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2009
    Messages : 561
    Par défaut
    Hi!

    May I ask why you are writing in English on a French forum?

    Could you please use the [ CODE ][ /CODE ] tag around your code to make it readable? And formatting your code may also help you understand your mistakes... Have a look at PSR2 coding standard, and tools to help you (php-cs-fixer from sensiolabs and squizlabs/php_codesniffer are what you should look for).

    Back to your problem:
    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
     
    function listAction()
    {
        $marques = array('BMW','Renault','Fiat');
        //list des voitures
        $voiture = array(
            array('Id' => 'C2345', 'serie' => '176', 'dateMiseCirculation' => '01/01/2014', 'Marque' => 'BMW'),
            array('Id' => 'A2345', 'serie' => '186', 'dateMiseCirculation' => '13/01/2015', 'marque' => 'Renault'),
            array('Id' => 'W0308', 'serie' => '196', 'dateMiseCirculation' => '02/11/2012', 'marque' => 'Fiat')
        );
        return $this->render(
            'MyAppEspritParcBundle:Voiture:list.html.twig',
            array('marques' => $marques, 'voiture' => $voiture)
        );
    }
    Here it is:
    1. The first car as an index Marque, the others marque, so it's different indexes
    2. $voiture should be $voitures (plural) as it is an array of objects


    Therefore:
    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
     
    function listAction()
    {
        $marques = array('BMW', 'Renault', 'Fiat');
        //list des voitures
        $voitures = array(
            array('id' => 'C2345', 'serie' => '176', 'dateMiseCirculation' => '01/01/2014', 'marque' => 'BMW'),
            array('id' => 'A2345', 'serie' => '186', 'dateMiseCirculation' => '13/01/2015', 'marque' => 'Renault'),
            array('id' => 'W0308', 'serie' => '196', 'dateMiseCirculation' => '02/11/2012', 'marque' => 'Fiat')
        );
        return $this->render(
            'MyAppEspritParcBundle:Voiture:list.html.twig',
            array('marques' => $marques, 'voitures' => $voitures)
        );
    }
    Then you have to change the view accordingly:
    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
     
        <h2>Liste des voitures</h2>
        <table border="1">
            <tr>
                <td>Id</td>
                <td>Série</td>
                <td>Date de Mise Circulation</td>
                <td>Marque</td>
            </tr>
            {%for voiture in voitures %}
            <tr>
                <td>{{ voiture.id }}</td>
                <td>{{ voiture.serie }}</td>
                <td>{{ voiture.dateMiseCirculation }}</td>
                <td>{{ voiture.marque }}</td>
            </tr>
            {% endfor %}
        </table>
    That is why it is so important to be consistent (variable names in lowerCamelCase, plurals for your collections).
    You should also use classes instead of arrays, so you can ensure id, serie, dateMiseCirculation and marque exist for each entity (here entity = subarray).

  3. #3
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Février 2015
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2015
    Messages : 8
    Par défaut
    it's done ..
    Thank you so much

Discussions similaires

  1. Cherche un framework en PHP
    Par wormseric dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 21/07/2007, 21h53
  2. Zend Framework avec PHP en mode CGI
    Par charles.caron dans le forum Zend Framework
    Réponses: 4
    Dernier message: 06/06/2007, 13h38
  3. Appel à contributions pour un framework web PHP
    Par Invité dans le forum Bibliothèques et frameworks
    Réponses: 3
    Dernier message: 02/09/2006, 15h37
  4. [Frameworks] Ajax PHP
    Par marcha dans le forum Bibliothèques et frameworks
    Réponses: 9
    Dernier message: 09/06/2006, 11h50

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